How to Use a DataTable in VB.NET

Written by jim campbell
  • Share
  • Tweet
  • Share
  • Email

The VB.NET "DataTable" control contains rows and columns of data you use to insert data in a DataGrid. A DataGrid displays the DataTable information in a formatted display. You must first create the DataTable columns, insert the data, then bind the DataTable to the DataGrid. A DataTable control can also contain multiple tables, so you can set up multiple DataGrids at once.

Skill level:
Moderate

Other People Are Reading

Instructions

  1. 1

    Right-click the "SLN" file, which is the main Visual Studio file that contains all your website code. Click "Open With," then double-click "Visual Studio."

  2. 2

    Double-click the code file that contains the DataGrid you need to set up. Start the code with a DataTable variable:

    Dim table As DataTable = new DataTable("MyTable")

  3. 3

    Set up the DataTable columns:

    column = New DataColumn()

    column.ColumnName = "first name"
    

    table.Columns.Add(column)

    column = New DataColumn()

    column.ColumnName = "last name"
    

    table.Columns.Add(column)

    You must create a column object for each field in the table, assign it a name and add the column to the DataTable.

  4. 4

    Add the data to the table:

    row = table.NewRow()

       row("first name") = "Joe"
    

    row("last name") = "Smith"

  5. 5

    Add the table to a data set object -- the data set lets you transfer the table to the DataGrid:

    ds = New DataSet()

    ds.Tables.Add(table)
    
  6. 6

    Fill the DataGrid with the DataTable information and bind the table to the DataGrid:

    grid.SetDataBinding(ds,"MyTable")

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.