Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 36946
  • Last Modified:

How to bind DataTable to DataGridView in VB.Net

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
itstheride
Asked:
itstheride
  • 4
1 Solution
 
TomMicrosoft ISV PartnerCommented:
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
 
itstherideSystems AdministrationAuthor Commented:
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
 
itstherideSystems AdministrationAuthor Commented:
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
 
itstherideSystems AdministrationAuthor Commented:
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
 
itstherideSystems AdministrationAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now