Datagridview Binding

slackey26

Member
Joined
Dec 5, 2005
Messages
5
Location
Missouri
Programming Experience
3-5
Ok, here's the problem. This is the first time I'm trying to use a datagridview in just straight VB.NET. I'm trying to bind it to a datareader but nothing gets displayed. I checked the reader to see if it had information in it and it does. So why isn't the datagrid showing anything?

VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strSql [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#800000]""[/COLOR][/SIZE]
[SIZE=2][COLOR=#800000][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sqlConnect [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] SqlConnection
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sqlReader [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] SqlDataReader[/SIZE]
[SIZE=2] 
[/SIZE][/COLOR][/SIZE][SIZE=2]strSql = wfnSearch()
 
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] strSql.Length <= 0 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Exit[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff] 
[/COLOR][/SIZE][SIZE=2]strSql = [/SIZE][SIZE=2][COLOR=#800000]"Select rtrim(FirstName) + ' ' + rtrim(LastName) "[/COLOR][/SIZE][SIZE=2] _
& [/SIZE][SIZE=2][COLOR=#800000]"From Contact_Info "[/COLOR][/SIZE][SIZE=2] _
& [/SIZE][SIZE=2][COLOR=#800000]"Where "[/COLOR][/SIZE][SIZE=2] & strSql[/SIZE]
[SIZE=2] 
sqlConnect = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlConnection()[/SIZE]
[SIZE=2] 
sqlConnect.ConnectionString = [/SIZE][SIZE=2][COLOR=#800000]"Server=myServer;Database=myDatabase;Trusted_Connection=True;"[/COLOR][/SIZE]
[SIZE=2][COLOR=#800000] 
[/COLOR][/SIZE][SIZE=2]sqlConnect.Open()
 
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sqlComm [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlCommand(strSql, sqlConnect)[/SIZE]
[SIZE=2] 
sqlReader = sqlComm.ExecuteReader
[/SIZE][SIZE=2] 
dgResults.DataSource = sqlReader

Everything that I have looked up uses a dataset. I don't want to use a dataset. All I want to happen is when I do a simple select, the results get displayed in the datagridview. Do I have to create columns because I thought the datagridview would do that when it gets bound?[/SIZE]
 
You Can't

Try Google, search bind DataReader to DataGridView
(Not trying to be a jerk just an enabler)
But in two words "You Can't"

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=359009&SiteID=1

Here are a couple lines that will replace


VB.NET:
sqlConnect.Open()
 
Dim sqlComm AsNew SqlCommand(strSql, sqlConnect)

sqlReader = sqlComm.ExecuteReader

dgResults.DataSource = sqlReader
 
'Replace with
 
Dim sqlDa as New SqlDataAdapter(strSql, sqlConnect)
Dim dt as new DataTable
sqlDa.Fill(dt)
dgResults.DataSourse = dt
 
'thats all same amount of lines no extra work

 
Last edited by a moderator:
humm yeah.. you need to get clear on this:

datareaders = forward only stream of data. efficient for lot of reading

datatable = a tabular block of data. efficient for lot of writing or random access


datagridview = a view of a data model, like a tabular block of data..




typically you would use a reader if you were, e.g. writing a text file in some format, line by line, and working through a reader, row by row

the datagrid view is meant for viewing a table of data.. so, use a datatable :) not a reader :)
 
Back
Top