Solved

How to bind DataTable to DataGridView in VB.Net

Posted on 2009-05-07
5
35,130 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle Mulit-site configuration 28 74
dat and idx extensions 11 43
Errror when importing data from Oracle to SQL 6 69
EF5 How do I stop pre-compiled views? 8 57
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

710 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