Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

How to bind DataTable to DataGridView in VB.Net

Avatar of itstheride
itstherideFlag for United States of America asked on
DatabasesVisual Basic Classic
5 Comments1 Solution39458 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
Avatar of itstheride
itstherideFlag of United States of America imageSystems Administration

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answers