Load data from a datatable into a DataGridView

Im trying to load data from a datatable into a DataGridView in a Windows app in Visual Studio 2005 (vb.net).  Theres something about OOP Im not understanding here, so its not loading the data from the datatable into the DataGridView.  Can you see where Im missing it and correct me?   Thank you!

First, I have a function that returns a datatable.  I know it works since I count the rows and the count equals the number of rows in the database table:
Public Function GetPubsTable() As DataTable
 Dim dtPubs As New DataTable
 [code that builds datatable . . .]
 Return dtPubs
End Function

I then attempt to load the DataGridView using the above function, but it just stays empty and generates no error:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim bsCustomers As BindingSource
    bsCustomers = New BindingSource
    Dim Dt As DataTable = New DataTable
    Dt = GetPubsTable()
    Dim theCount As Integer = 0
    theCount = Dt.Rows.Count      The count correctly counts the number of rows in the table.
    BindingSource1.DataSource = Dt
    DataGridView1.DataSource = bsCustomers
End Sub

In case it helps, I show the data from the test database and the table definition:
1      SmithO            123 Main St      KS
2      SilverLake            Lake Road      KS
3      Flako Company      Corn Street      OK

CustomerID            int      
CustomerName      varchar(50)      
CustomerAddress      varchar(50)      
CustomerState      char(2)      
psmithphilAsked:
Who is Participating?
 
YZlatConnect With a Mentor Commented:
replace
DataGridView1.DataSource = bsCustomers
with

DataGridView1.DataSource = Dt
0
 
YZlatCommented:
also I am not sure what do you need this line for:

  BindingSource1.DataSource = Dt
0
 
VBRocksCommented:
Definitely you can make a change here:

   Dim bsCustomers As BindingSource
    bsCustomers = New BindingSource

    Dim Dt As DataTable = GetPubsTable()

    Dim theCount As Integer = 0
    theCount = Dt.Rows.Count      The count correctly counts the number of rows in the table.

    bsCustomers.DataSource = Dt
    DataGridView1.DataSource = bsCustomers


'Also, you don't need to use a BindingSource, if you don't want to, so you can literally just do this:
    Dim Dt as DataTable = GetPubsTable()
    Dim theCount As Integer = Dt.Rows.Count
    DataGridView1.DataSource = Dt


0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
YZlatCommented:
VBRocks, don't you think you are repeating the same thing I mentioned half an hour ago?
0
 
VBRocksCommented:
Sorry YZlat.  I didn't intend on repeating your code.  I did try to give an example that cuts out some
of the extra lines of code they included.

My apologies.

0
 
psmithphilAuthor Commented:
YZlat, I was killing myself trying to get the bindingsource to work.  I was sure making it harder than it had to be.  I'd better do some studying to make sure I understand the "why" behind this and why I missed it.  It works great.  Thank you very much for your help!

And thank you for your comment also, VBRocks, although similar to YZlat.

I will probably have another question soon since I'm going to bump my head with a dataset next.
0
 
psmithphilAuthor Commented:
In case this helps someone, I tried this same thing setting the function as a DataSet instead of a DataTable.  The names of some of the variables are weird, but it still shows what was done.

Public Function GetDataSet() As DataSet
   [Code for sql connection, etc. . . ]
   oDataAdapterTitles = New SqlDataAdapter()
                oDataAdapterTitles.SelectCommand = oSqlCommPubs
                'Create and fill a Data Set
                Dim dsInfo As DataSet = New DataSet()
                oDataAdapterTitles.Fill(dsInfo, "Customer") 'Needed for DGV to show (Source Table needed).
                'oDataAdapterTitles.Fill(dsInfo)    'This line fails since it is without the Source Table as above.
                Return dsInfo

'Then the function is called:
Private Sub btnbtnDataSet_Click(ByVal sender As System. . . etc)
    Dim Ds As DataSet
    Ds = New DataSet
    Ds = GetDataSet()   'Call function.
   DataGridView1.AutoGenerateColumns = True
    DataGridView1.DataSource = Ds
   'This line is necessary for DGV to show data, along with Source Table line in function:
    DataGridView1.DataMember = "Customer"
End Sub
0
 
VBRocksCommented:
You can also directly set the DataSource, like this:

    DataGridView1.DataSource = Ds.Tables("Customer")

    'This line is now obselete
    'DataGridView1.DataMember = "Customer"

0
 
psmithphilAuthor Commented:
Thank you, VBRocks.   I will keep your line in my mind to make sure I understand what's going on with it.  For now I'm going to use the extra line (.DataMember =) to insure I see how it working.  I wish I had your knowledge.
0
 
VBRocksCommented:
Just as an idea, you might want to set the DataMember first, then the DataSource.  It's a little more
optimized.  

Example:
    DataGridView1.DataMember = "Customer"
    DataGridView1.DataSource = Ds.Tables("Customer")

   
0
 
VBRocksCommented:
Oops, sorry...  scratch that example and use this one:

Example:
    DataGridView1.DataMember = "Customer"
    DataGridView1.DataSource = Ds

0
 
psmithphilAuthor Commented:
Thank you VBRocks!   These "little" things can make a big difference in things like performance, etc.  I appreciate you sharing your knowledge!

Wouldn't it be nice to edit posts we make?  I noticed I left out "End Function" in my post.  It would have been nice to add it, but I'm not going to make a new post just to correct something that small.
0
 
VBRocksCommented:
You're welcome!  Glad I could help.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.