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 DataGridView control use a DataTable built at run time in Visual Basic 2005
DescriptionThis example shows how to make a DataGridView control use a DataTable built at run time in Visual Basic 2005.
KeywordsDataGridView, DataTable, VB 2005
CategoriesDatabase, Controls
 
When its form loads, the program makes a DataTable object and adds some columns to it. It then uses the object's Rows collection's Add method to add arrays of objects to the DataTable. It finishes by setting the DataGridView's DataSource property to the DataTable. The DataTable does the rest automatically.
 
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", "Nerd"})
    dt.Rows.Add(New Object() {"Sergio", "Aragones", _
        "Cartoonist"})
    dt.Rows.Add(New Object() {"Eoin", "Colfer", "Author"})
    dt.Rows.Add(New Object() {"Terry", "Pratchett", "?"})

    DataGridView1.DataSource = dt
End Sub
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated