?
Solved

Bind DataTable to DataGridView

Posted on 2011-04-22
6
Medium Priority
?
737 Views
Last Modified: 2012-05-11
I have a form that loads with a DataGridView and I want to create a datatable and bind to the DGV so that the user can enter data....see code below...

I'm so frustrated....I can't figure out how to configure this form for data entry....

Private Sub Bind_Labor_Data_Table()

        Dim dt_LaborInfo As DataTable = New DataTable("Labor")

        dt_LaborInfo.Columns.Add("Row_ID")
        dt_LaborInfo.Columns.Add("Slip_Number")
        dt_LaborInfo.Columns.Add("Job_Number")
        dt_LaborInfo.Columns.Add("SWO_ID")
        dt_LaborInfo.Columns.Add("Labor_Name")
        dt_LaborInfo.Columns.Add("Classification")
        dt_LaborInfo.Columns.Add("Labor_Rate")
        dt_LaborInfo.Columns.Add("Hours")
        dt_LaborInfo.Columns.Add("Hours_Type")

        dt_LaborInfo.Rows.Add()

        Me.DS_Labor.Tables.Add(dt_LaborInfo)
        Me.DG_Labor.DataSource = Me.DS_Labor.Tables("Labor")
        Me.DG_Labor.DataMember = "Row_ID"

    End Sub
0
Comment
Question by:tobin46
  • 4
  • 2
6 Comments
 
LVL 1

Author Comment

by:tobin46
ID: 35451668
Sorry....when form loads, I call Bind_Labor_Data_Table() but the DGV is blank....



0
 
LVL 1

Accepted Solution

by:
tobin46 earned 0 total points
ID: 35451725
I figured it out...had to incorporate the Bindingsource...

Private Sub Bind_Labor_Data_Table()

        Dim dt_LaborInfo As DataTable = Me.DS_Labor.Tables.Add("Labor")

        dt_LaborInfo.Columns.Add("Row_ID")
        dt_LaborInfo.Columns.Add("Slip_Number")
        dt_LaborInfo.Columns.Add("Job_Number")
        dt_LaborInfo.Columns.Add("SWO_ID")
        dt_LaborInfo.Columns.Add("Labor_Name")
        dt_LaborInfo.Columns.Add("Classification")
        dt_LaborInfo.Columns.Add("Labor_Rate")
        dt_LaborInfo.Columns.Add("Hours")
        dt_LaborInfo.Columns.Add("Hours_Type")

       
        Me.DG_Labor_Binding.DataSource = Me.DS_Labor.Tables("Labor")
        Me.DG_Labor.DataSource = Me.DG_Labor_Binding
0
 
LVL 1

Author Closing Comment

by:tobin46
ID: 35451726
I solved the problem myself.
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 64

Expert Comment

by:Fernando Soto
ID: 35451728
Hi tobin46;

The issue is with the DataMember property.  When used must me the name of the table in the DataSource. So you can so this:
 
        Me.DS_Labor.Tables.Add(dt_LaborInfo)
        Me.DG_Labor.DataSource = Me.DS_Labor
        Me.DG_Labor.DataMember = "Labor"

or this:

        Me.DS_Labor.Tables.Add(dt_LaborInfo)
        Me.DG_Labor.DataSource = Me.DS_Labor.Tables("Labor")

or even this, but this is redundant :

        Me.DS_Labor.Tables.Add(dt_LaborInfo)
        Me.DG_Labor.DataSource = Me.DS_Labor.Tables("Labor")
        Me.DG_Labor.DataMember = "Labor"

If you only have one table you do not need to use a DataSet just use the DataTable as the DataSource.

Fernando
0
 
LVL 1

Author Comment

by:tobin46
ID: 35451793
Fernando...I tried options 1 and 2....and the datagridview was blank both times.....
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 35453009
Hi tobin46;

I have just completed testing the three options I posted in my last post and all three options work on my system.

From Microsoft Documentation:
DataGridView.DataMember Property
This property is useful when binding to a data source that contains multiple lists or tables. You do not need to set this property when binding to a data source that contains a single list or table. For example, you can bind a DataGridView control to a DataSet that contains a single table without setting this property. If the DataSet contains multiple tables, however, you must set this property to the name of one of the tables.

Fernando
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

850 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