?
Solved

Load data from a datatable into a DataGridView

Posted on 2007-10-04
13
Medium Priority
?
6,883 Views
Last Modified: 2013-11-26
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)      
0
Comment
Question by:psmithphil
  • 6
  • 4
  • 3
13 Comments
 
LVL 35

Accepted Solution

by:
YZlat earned 2000 total points
ID: 20016374
replace
DataGridView1.DataSource = bsCustomers
with

DataGridView1.DataSource = Dt
0
 
LVL 35

Expert Comment

by:YZlat
ID: 20016381
also I am not sure what do you need this line for:

  BindingSource1.DataSource = Dt
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 20016657
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 35

Expert Comment

by:YZlat
ID: 20016937
VBRocks, don't you think you are repeating the same thing I mentioned half an hour ago?
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 20017094
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
 

Author Comment

by:psmithphil
ID: 20017110
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
 

Author Comment

by:psmithphil
ID: 20018009
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
 
LVL 27

Expert Comment

by:VBRocks
ID: 20018085
You can also directly set the DataSource, like this:

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

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

0
 

Author Comment

by:psmithphil
ID: 20018530
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
 
LVL 27

Expert Comment

by:VBRocks
ID: 20023804
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
 
LVL 27

Expert Comment

by:VBRocks
ID: 20023810
Oops, sorry...  scratch that example and use this one:

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

0
 

Author Comment

by:psmithphil
ID: 20025345
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
 
LVL 27

Expert Comment

by:VBRocks
ID: 20025612
You're welcome!  Glad I could help.

0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question