How to Use a DataTable in VB.NET
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.
Instructions
-
-
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
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
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
Add the data to the table:
row = table.NewRow()
row("first name") = "Joe"
row("last name") = "Smith"
-
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
Fill the DataGrid with the DataTable information and bind the table to the DataGrid:
grid.SetDataBinding(ds,"MyTable")
-
1