We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Bind DataTable to DataGridView

tobin46
tobin46 asked
on
Medium Priority
834 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
Comment
Watch Question

Author

Commented:
Sorry....when form loads, I call Bind_Labor_Data_Table() but the DGV is blank....



Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
I solved the problem myself.
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
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

Author

Commented:
Fernando...I tried options 1 and 2....and the datagridview was blank both times.....
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
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
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.