Binding to a DataGrid Control
To finish off the DataAccess application, you're going to bind a DataSet to a DataGrid control. To set this up, you need to add a DataGrid and ComboBox control to the TabControl1 control.Follow these steps to set up the rest of the project:
- In design mode, click the second tab on the TabControl1 control.
- Drag a ComboBox control from the Toolbox to the TabPage2 control.
- Drag a DataGrid control from the Toolbox to the TabPage2 control.
Your form should resemble Figure 10.8.
Figure 10.8. Form after adding DataGrid and ComboBox controls to TabPage2.

Next, you must write the code to fill the DataGrid with data from a DataSet. The ComboBox is there for the exercises at the end of the day; you won't do anything with it now.To fill the DataGrid, you're going to write code in the TabIndexChanged event of the TabControl1 control. To write the code that loads the grid, double-click TabControl1 to get to the TabControl1_TabIndexChanged event. The code in Listing 10.9 binds the DataGrid control with the data from the Orders table in the Northwind database.
Listing 10.9 TabControl1_SelectedIndexChanged Event Code
Private Sub TabControl1_TabIndexChanged _
(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles TabControl1.SelectedIndexChanged
' Check to see what tab we are on,
' if it is the 2nd tab, then do the databinding for the grid
If TabControl1.SelectedTab.TabIndex = 1 Then
' Create a new connection object
Dim cn As New SqlConnection( _
"Server=(local)\NetSDK;DataBase=northwind;" _
& "Integrated Security=SSPI")
' Create a new SqlDataAdapter and pass the SQL Statement
' and connection object
Dim da As SqlDataAdapter = New SqlDataAdapter _
("SELECT * from Orders", cn)
' Create a DataSet to hold the SqlDataAdapter data
Dim ds As DataSet = New DataSet("Orders")
' Call the Fill method to load the DataSet
da.Fill(ds)
' Set the DataSource property of the grid
' to bind the data from the DataSet
DataGrid1.DataSource = ds
End If
End Sub
private void tabControl1_TabIndexChanged(object sendert,
System.EventArgs e)
{
// Check to see what tab we are on,
// if it is the 2nd tab, then do the databinding for the grid
if (tabControl1.SelectedTab.Text == "DataGrid Binding");
{
// Get the connection
SqlConnection cn = new SqlConnection
(@"Server=(local)\NetSDK;DataBase=northwind;
Integrated Security=SSPI");
// Create a new SqlDataAdapter and pass the SQL Statement
// and connection object
SqlDataAdapter da = new SqlDataAdapter
("SELECT * from Orders", cn);
// Create a DataSet to hold the SqlDataAdapter data
DataSet ds = new DataSet("Orders");
// Call the Fill method to load the DataSet
da.Fill(ds);
// Set the DataSource property of the grid
// to bind the data from the DataSet
dataGrid1.DataSource = ds;
}
}
The code is identical to all the SqlDataAdapter code you've written today. You create a Connection and a Command, and call Fill on the DataAdapter. The only difference is that you set the DataSource property of the DataGrid to the DataSet object.If you run the code by pressing F5 and click the second tab, you should see results like Figure 10.9.
Figure 10.9. Complex binding to a DataGrid control.

Like magic, you have a DataGrid filled with data from a SQL Server database.As you can see, implementing simple and complex databinding is very simple. By setting a few properties in any control in the Toolbox, you can quickly create a data-driven user interface.
• Table of Contents
• Index
Sams Teach Yourself Visual Studio® .NET 2003 in 21 Days
By
Jason Beres
Publisher
: Sams Publishing
Pub Date
: January 14, 2003
ISBN
: 0-672-32421-0
Pages
: 696
Sams Teach Yourself Visual Studio .NET in 21 Days will help developers that are new to application development and experienced developers understand how to use the .NET Framework and Visual Studio .NET to rapidly develop any type of computer application. The Visual Studio .NET development environment is the most comprehensive developer tool ever created, putting that together with the .NET Frameworks' Class Libraries, the developer has everything he or she needs to get up-to-speed on Microsoft's latest revolution in application development. This book will guide the developer through using the VS .NET IDE, the Visual Basic .NET and C# language, and the supporting tools available from Microsoft to create Windows and Web-based applications. The market is full of books that pretty much say the same thing, which is already available in the help files, the author of this book has written and deployed over a dozen successful applications using Visual Studio .NET and the .NET Framework. All of his expertise and experience is used to give you the most comprehensive title on using Visual Studio .NET.