Solved

How to bind DataTable to DataGridView in VB.Net

Posted on 2009-05-07
5
34,742 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
ID: 24330852
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
ID: 24335915
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
ID: 24336352
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
ID: 24337150
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
ID: 24338868
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

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