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.
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.
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.
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.