O'Reilly logo

Programming Visual Basic .NET, Second Edition by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Working with Data-Bound Controls

ADO.NET provides good support for “data-bound” objects (that is, objects that can be tied to a particular data set, such as those retrieved from a database by ADO.NET).

A simple example of a data-bound control is the DataGrid control provided with both Windows Forms and Web Forms.

Populating a DataGrid

In its simplest use, a DataGrid is easy to implement. Once again, first create a DataSet and then fill it from the Customers table of the Northwind database, but this time, rather than iterating through the rows of the data set and writing the output to a listbox, you can simply bind the Customers table in your data set to a DataGrid control.

To illustrate, alter Example 14-1 by deleting the listbox from the form you created and replace it with a DataGrid. The default name provided by the Visual Studio design tool is DataGrid1, but let’s change it to CustomerDataGrid. After the data set is created and filled, you bind the DataGrid through its DataSource property:

CustomerDataGrid.DataSource =
    myDataSet.Tables("Customers").DefaultView

Example 14-3 provides the complete source code for this example.

Example 14-3. Using a DataGrid

Option Strict On Imports System Imports System.Drawing Imports System.Collections Imports System.ComponentModel Imports System.Windows.Forms Imports System.Data Imports System.Data.SqlClient Public Class ADOForm3 Inherits System.Windows.Forms.Form Private components As System.ComponentModel.Container Friend WithEvents CustomerDataGrid ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required