Skip to Content
Programming Visual Basic .NET
book

Programming Visual Basic .NET

by Dave Grundgeiger
December 2001
Beginner
464 pages
13h 51m
English
O'Reilly Media, Inc.
Content preview from Programming Visual Basic .NET

Binding a DataSet to a Windows Forms DataGrid

DataSet and DataTable objects can be bound to Windows Forms DataGrid objects to provide an easy way to view data. This is done by calling a DataGrid object’s SetDataBinding method, passing the object that is to be bound to the grid. The syntax of the SetDataBinding method is:

Public Sub SetDataBinding( _
   ByVal dataSource As Object, _
   ByVal dataMember As String _
)

The parameters are:

dataSource

The source of the data to show in the grid. This can be any object that exposes the System.Collections.IList or System.Data.IListSource interfaces, which includes the DataTable and DataSet classes discussed in this chapter.

dataMember

If the object passed in the dataSource parameter contains multiple tables, as a DataSet object does, the dataMember parameter identifies the table to display in the DataGrid. If a DataTable is passed in the dataSource parameter, the dataMember parameter should contain either Nothing or an empty string.

Example 8-8 shows how to bind a DataSource object to a DataGrid. The DataSource object contains a Customers table and an Orders table, and a relation between them. The call to the DataGrid object’s SetDataBinding method specifies that the Customers table should be shown in the grid. Figure 8-3 shows the resulting DataGrid display.

Example 8-8. Creating a DataSet and binding it to a Windows Forms DataGrid

' Open a database connection. Dim strConnection As String = _ "Data Source=localhost;Initial Catalog=Northwind;" ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming Visual Basic .NET, Second Edition

Programming Visual Basic .NET, Second Edition

Jesse Liberty

Publisher Resources

ISBN: 0596000936Supplemental ContentCatalog PageErrata