Solved

How to bind DataTable to DataGridView in VB.Net

Posted on 2009-05-07
5
34,125 Views
Last Modified: 2013-12-25
I am learning VB.Net. I am trying to write some generalized code that I can use with any datagridview of "y" rows by "x" columns to which I am binding a bunch of string data.  In the code below, the MemoryTable seems to create and populate fine with "Test Data".  However, when I get to binding, that is what doesn't work.  When the application loads the DataGriewView is just all gray, with no data.  If I comment out the section under "Bind" the Data Grid View loads fine with all the rows and columns.  Binding just doesn't seem to work.   There must be something I am missing so that the binding happens and the datagrid is populated.  I have tried several variations of binding from several Google searches and all yield a blank, gray DataGridView with just the column headings, in this case 1-30.
Private Sub BindDataTable()
 

        Dim y As Integer, x As Integer

        Dim MemoryTable As DataTable
 

        MemoryTable = New DataTable("GridTable")
 

        Dim DataRows(MaxRows) As DataRow

        Dim DataColumns(MaxColumns) As DataColumn
 

        For x = 1 To MaxColumns

            DataColumns(x - 1) = New DataColumn(CType(x, String))

            DataColumns(x - 1).DataType = System.Type.GetType("System.String")

            MemoryTable.Columns.Add(DataColumns(x - 1))

        Next
 

        For y = 1 To MaxRows

            DataRows(y - 1) = MemoryTable.NewRow

        Next
 

        For y = 1 To MaxRows

            For x = 1 To MaxColumns

                DataRows(y - 1).Item(CType(x, String)) = "Test Data"

            Next x

        Next y
 

        'Bind
 

        Dim Dset As New DataSet()

        Dset = New DataSet()

        Dset.Tables.Add(MemoryTable)

        DataGridMain.DataSource = Dset

        DataGridMain.DataMember = "GridTable"
 

    End Sub

Open in new window

0
Comment
Question by:itstheride
  • 4
5 Comments
 
LVL 9

Expert Comment

by:Tom
Comment Utility
Have a look at the
Forms over Data Video Series from msdn
http://msdn.microsoft.com/en-us/vbasic/bb466226.aspx?wt.slv=RightRail

It will show you all the tips and tricks
0
 
LVL 2

Author Comment

by:itstheride
Comment Utility
Not seeing the answer in these videos.  I am trying to do this in my own code for some good reasons, without having Visual Studio write the code behind the scenes by clicking this or that.  That seems to be where most of these videos go.  I am sure I am just missing something simple.
0
 
LVL 2

Author Comment

by:itstheride
Comment Utility
Here are the three ways I saw to do binding...none seem to work for me.  Is there something about my Datagridview that is preventing the binding?  I read something about DataPropertyName...but not sure how that relates.  My DataGridView has the same number of rows and columns as the in memory datatable and the same column names which are just numbers 1-30.   There has to be something simple I am just missing here.


        'Bind Option #1
 

        Dim Dset As New DataSet()

        Dset = New DataSet()

        Dset.Tables.Add(MemoryTable)

        DataGridMain.DataSource = Dset

        DataGridMain.DataMember = "GridTable"
 

        'Bind Option #2
 

        Dim Dset As New DataSet()

        Dset = New DataSet()

        Dset.Tables.Add(MemoryTable)

        DataGridMain.DataSource = Dset.Tables("GridTable")
 

        'Bind Option #3
 

        Dim Dset As New DataSet()

        Dset = New DataSet()

        Dset.Tables.Add(MemoryTable)

        Dim bs As BindingSource

        bs = New BindingSource()

        bs.DataSource = Dset

        bs.DataMember = Dset.Tables(0).TableName

        DataGridMain.DataSource = bs

        DataGridMain.Refresh()

Open in new window

0
 
LVL 2

Accepted Solution

by:
itstheride earned 0 total points
Comment Utility
Figured it out myself.  Here is a smaller, less complicated test program that works and does the binding.  Apparently has to do with the way one adds rows in the datatable--this approach works.  The DataPropertyName must match the Column name in the DataTable as well.   Maybe this will help someone else.  To test this program just add a DataGridView Control to a form and add one TextBoxColumn.  So no points.
Public Class Form1
 

    Private dt As DataTable

    Private ds As DataSet
 
 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 

        Dim Row1 As DataRow
 

        DataGridView1.Columns(0).DataPropertyName = "Name"
 

        dt = New DataTable("mytable")

        dt.Columns.Add("Name")

        dt.Rows.Add()

        Row1 = dt.Rows(0)

        Row1.Item("Name") = "John Doe"
 

        ds = New DataSet()

        ds.Tables.Add(dt)

        DataGridView1.DataSource = ds

        DataGridView1.DataMember = "mytable"
 

    End Sub
 

End Class

Open in new window

0
 
LVL 2

Author Comment

by:itstheride
Comment Utility
So my original "generalized" sub is changed as below and binding works just great now.
 Private Sub BindDataTables()
 

        Dim y As Integer, x As Integer
 

        'Main Grid
 

        MemoryTableMain = New DataTable("MainGridTable")
 

        Dim DataColumnsMain(MaxColumns) As DataColumn
 

        'Add Columns
 

        For x = 1 To MaxColumns

            DataColumnsMain(x - 1) = New DataColumn(CType(x, String))

            DataColumnsMain(x - 1).DataType = System.Type.GetType("System.String")

            MemoryTableMain.Columns.Add(DataColumnsMain(x - 1))

        Next
 

        'Add Rows
 

        For y = 1 To MaxRows

            MemoryTableMain.Rows.Add()

        Next
 

        'Bind
 

        Dim DsetMain As New DataSet()

        DsetMain = New DataSet()

        DsetMain.Tables.Add(MemoryTableMain)

        DataGridMain.DataSource = DsetMain

        DataGridMain.DataMember = "MainGridTable"
 

End Sub

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now