Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake a DataTable at run time without a database and bind it to a DataGrid in VB .NET
DescriptionThis example shows how to make a DataTable at run time without a database and bind it to a DataGrid in VB .NET.
Keywords
CategoriesDatabase, VB.NET
 
When the form loads, the program creates a new DataTable object. It uses the object's Columns collection's Add method to define the table's columns. It then uses the Rows.Add method to add rows to the DataTable. This method takes as a parameter an array of Objects and uses them to fill in the columns.

After it has created and populated the DataTable, the program assigns it to the DataGrid's DataSource property. The grid automatically displays the data and lets the user edit it.

 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    Dim dt As New DataTable("People")
    dt.Columns.Add("First Name")
    dt.Columns.Add("Last Name")
    dt.Columns.Add("Occupation")

    dt.Rows.Add(New Object() {"Rod", "Stephens", "Computer " & _
        "Nerd"})
    dt.Rows.Add(New Object() {"Sergio", "Aragones", _
        "Cartoonist"})
    dt.Rows.Add(New Object() {"Eoin", "Colfer", "Author"})
    dt.Rows.Add(New Object() {"Terry", "Pratchett", "?"})

    DataGrid1.DataSource = dt
End Sub
 
This example does not save the data anywhere. It is more an example of how to display grid data without a database.

For more information on database programming in VB .NET, see my book Visual Basic .NET Database Programming.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated