|
|
Title | Display the data in a DataSet by using as DataGrid control in VB .NET |
Description | This example shows how to display the data in a DataSet by using as DataGrid control in VB .NET. |
Keywords | DataSet, DataGrid, VB.NET, data, database, ADO.NET |
Categories | Database, VB.NET, Controls |
|
|
First, create a DataSet for the database. Also create a DataAdapter for each table that you want to load into the DataSet.
- Create the form and add two OleDbDataAdapters. Configure them to select the Students and Scores tables from the database. (Note that you will probably need to reconfigure the example program's adapters to find the new database location.)
- Select the adapters and select the Data menu's Generate DataSet command. Give the DataSet a meaningful type such as TestScoresDataSet, verify that both of the DataAdapters are checks, and click OK.
- Rename the instance of the DataSet that was placed on the form. For example, call it dsTestScores.
Next add a DataGrid to the program's form. Set its DataSource property to the DataSet.
Alternatively, you can set the DataSource to one of the DataSet's tables. If you do that, the DataGrid will only display records in that table. If you set DataSource to the whole DataSet, the DataGrid will let the user explore all of the tables.
When the program starts, it uses the following code to load the data into the DataSet.
That's all there is to it! The DataGrid provides all of the navigation through the DataSet automatically.
|
|
' Load the DataSet.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles MyBase.Load
daStudents.Fill(dsTestScores)
daScores.Fill(dsTestScores)
End Sub
|
|
For more information on database programming in VB .NET, see my book Visual Basic .NET Database Programming.
|
|
|
|
|
|