Data Binding

only for RuBoard

Data binding in .NET is different than it was in previous Microsoft products. In .NET, data binding means binding any property of any .NET control to any publicly available information. It accesses data in the form of classes that expose raw data as properties and, in many cases, defines methods for updating the data. "Data" is a rather broad term in .NET. It might be values from a data store, names of controls from a collection, or attribute values from an object property. As you can see, this requires a slight shift in thinking from previous data access methods.

Complex controls that include embedded controls, such as the DataGrid , can bind to any class that supports the ICollection interface, such as a DataTable , DataReader , array, collection, or an object's properties. The ICollection interface defines size , enumerators, and synchronization methods for all collections, and it guarantees that the data class provides at least a basic means of data access and data navigation.

One example of an object that supports the ICollection interface is the DataSet object. One of its collections is the Tables collection, which supports the ICollection interface. This means that you could use the DataSet.Tables collection as a data source for a server control to render a list of all DataTables in the current DataSet .

Binding a data source to a DataGrid requires three steps:

  1. Define a data source.

  2. Set the DataGrid 's DataSource property to the data source class you defined.

  3. Call the DataBind() method.

Define a Data Source

In Chapter 3, you learned how to use the Managed Providers to retrieve data from a data source such as Microsoft's SQL Server. While this is the most typical type of data you'll be binding to, you could also bind a DataGrid to any other publicly available data. We'll get into other data sources later in this chapter. For now, let's look at how to bind to data from a database. Listing 5.2 shows how to create two DataTable classes in a DataSet .

Listing 5.2 Creating a Data Source with the SQL Managed Provider
 [VB] 01: <%@ Page Language="VB" %> 02: <%@ Import Namespace="System.Data" %> 03: <%@ Import Namespace="System.Data.SqlClient" %> 04: 05: <script runat="server"> 06:   Sub Page_Load(Sender As Object, E As EventArgs) 07:     Dim myDataSet As New DataSet 08:     Dim myDataAdapter As SqlDataAdapter 09: 10:     myDataAdapter = New SqlDataAdapter("SELECT * FROM Customers", graphics/ccc.gif "server=localhost;database=Northwind;uid=sa;pwd=;") 11:     myDataAdapter.Fill(myDataSet, "Customers") 12: 13:     myDataAdapter.SelectCommand.CommandText = "SELECT * FROM Products" 14:     myDataAdapter.Fill(myDataSet, "Products") 15:   End Sub 16: </script> [C#] 01: <%@ Page Language="C#" %> 02: <%@ Import Namespace="System.Data" %> 03: <%@ Import Namespace="System.Data.SqlClient" %> 04: 05: <script runat="server"> 06:   void Page_Load(Object sender, EventArgs e){ 07:     DataSet myDataSet = new DataSet(); 08:     SqlDataAdapter myDataAdapter; 09: 10:     myDataAdapter = new SqlDataAdapter("SELECT * FROM Customers", graphics/ccc.gif "server=localhost;database=Northwind;uid=sa;pwd=;"); 11:     myDataAdapter.Fill(myDataSet, "Customers"); 12: 13:     myDataAdapter.SelectCommand.CommandText = "SELECT * FROM Products"; 14:     myDataAdapter.Fill(myDataSet, "Products"); 15:   } 16: </script> [VB & C#] 17: <html> 18: <head> 19: <title>Programming Datadriven Web Applications with ASP.NET - Chapter 5</title> 20: </head> 21: <body> 22:  <asp:DataGrid id="myDataGrid" runat="server" /> 23: </body> 24: </html> 

On lines 2 and 3 of Listing 5.2, you import the namespaces necessary for using the basic data functionality and the SQL Managed Provider. On line 7, you create a new instance of the DataSet class, inside the Page_Load() event handler. You follow that on line 8 by declaring a new instance of the SqlDataAdapter class. On line 10 you create the instance of the SqlDataAdapter , passing in a SQL SELECT statement and a connection string. On line 11, you execute the SQL statement by calling the Fill() method of the SqlDataAdapter . The result of the Fill() method is a new DataTable object in the DataSet . On line 13 you reset the SelectCommand.CommandText property of the SqlDataAdapter to a new SQL SELECT statement. Since the connection string is not changing for this new SQL statement, you only need to reset the CommandText property of the SelectCommand in the SqlDataAdapter . On line 14 you use the Fill() method again to execute the SQL statement, and create a new (second) DataTable in the DataSet .

Set the DataSource of the DataGrid

In the preceding example, you created two DataTable objects in the DataSet . Both of these DataTables are suitable candidates as a data source for the DataGrid . On the other hand, the DataSet.Tables collection is also a suitable candidate as the data source. Before you use an obvious data source for the DataGrid , such as a DataTable , you should know how to bind to a collection. This will teach you more about how the collection is built.

In Listing 5.2, insert the following code after line 14 and before line 15:

 [VB] 14a:      myDataGrid.DataSource = myDataSet.Tables [C#] 14a:      myDataGrid.DataSource = myDataSet.Tables; 

Line 14a programmatically sets the DataSource property of the DataGrid to the Tables collection of the DataSet object. Now, there's one last step before you can see the fruits of your labor.

Call the DataBind() Method

The DataBind() method executes the binding of the control to the data source. When this method is called, a row is generated for each record in the data source and a cell is generated in each row for each column in the data source. The DataBind() method can be called by the individual data server control ( myDataGrid.DataBind() ) or by the Page object ( Page.DataBind() ), which will bind any child control(s) that has a data source defined.

Note

The DataBind() method is read-only. In Chapter 10 you'll learn how to create your own logic to update the data store with new values.


The following line of code, inserted between line 14a and line 15 in Listing 5.2, will call the DataBind() method on the DataGrid object:

 [VB] 14b:      myDataGrid.DataBind() [C#] 14b:      myDataGrid.DataBind(); 

The DataBind() method call executes the built-in data binding logic. When the page is viewed in a browser, a basic HTML table is rendered displaying all of the information about the two DataTable objects in the DataSet.Tables collection. Figure 5.1 shows this page in Internet Explorer 6.0.

Figure 5.1. The DataGrid is bound to the DataSet.Tables collection. It contains all the information about the collection.
graphics/05fig01.gif

Binding to a DataTable in a DataSet

In the preceding example, you bound the DataGrid output to the DataSet.Tables collection, causing a table with a row for each DataTable to be rendered. This is cool and all, but it's not terribly useful. What you really want to do is display the data from one of the DataTable s in the DataGrid , or display multiple DataGrid s, each one with the data from a different DataTable .

Listing 5.3 shows the ASP.NET Web Form from Listing 5.2 with the Customers DataTable bound to the DataGrid .

Listing 5.3 Binding a DataGrid to a DataTable
 [VB] 01: <%@ Page Language="VB" %> 02: <%@ Import Namespace="System.Data" %> 03: <%@ Import Namespace="System.Data.SqlClient" %> 04: 05: <script runat="server"> 06:   Sub Page_Load(Sender As Object, E As EventArgs) 07:     Dim myDataSet As New DataSet 08:     Dim myDataAdapter As SqlDataAdapter 09: 10:     myDataAdapter = New SqlDataAdapter("SELECT * FROM Customers", graphics/ccc.gif "server=localhost;database=Northwind;uid=sa;pwd=;") 11:     myDataAdapter.Fill(myDataSet, "Customers") 12: 13:     myDataAdapter.SelectCommand.CommandText = "SELECT * FROM Products" 14:     myDataAdapter.Fill(myDataSet, "Products") 15: 16:     myDataGrid.DataSource = myDataSet.Tables("Customers") 17:     myDataGrid.DataBind() 18:   End Sub 19: </script> [C#] 01: <%@ Page Language="C#" %> 02: <%@ Import Namespace="System.Data" %> 03: <%@ Import Namespace="System.Data.SqlClient" %> 04: 05: <script runat="server"> 06:   void Page_Load(Object sender, EventArgs e){ 07:     DataSet myDataSet = new DataSet(); 08:     SqlDataAdapter myDataAdapter; 09: 10:     myDataAdapter = new SqlDataAdapter("SELECT * FROM Customers", graphics/ccc.gif "server=localhost;database=Northwind;uid=sa;pwd=;"); 11:     myDataAdapter.Fill(myDataSet, "Customers"); 12: 13:     myDataAdapter.SelectCommand.CommandText = "SELECT * FROM Products"; 14:     myDataAdapter.Fill(myDataSet, "Products"); 15: 16:     myDataGrid.DataSource = myDataSet.Tables["Customers"]; 17:     myDataGrid.DataBind(); 18:   } 19: </script> [VB & C#] 20: <html> 21: <head> 22: <title>Programming Datadriven Web Applications with ASP.NET - Chapter 5</title> 23: </head> 24: <body> 25:  <asp:DataGrid id="myDataGrid" runat="server" /> 26: </body> 27: </html> 

For the most part, Listing 5.3 works the same as Listing 5.2. The only real change is on line 16, where you set the DataSource property of the DataGrid to the Customers DataTable in the DataSet . The resulting Web page renders a row for each DataRow in the DataTable and a cell in each row for each DataColumn (see Figure 5.2).

Figure 5.2. A basic DataGrid renders a row for each DataRow in the DataTable and a cell in each row for each DataColumn .
graphics/05fig02.gif

To display multiple DataGrid s, each one bound to a different DataTable in the DataSet , you only need to define the DataSource for each DataGrid , and use the Page.DataBind() method to bind all data server controls on the page to their data source. Listing 5.4 changes the SQL statement to return only five rows for each DataTable .

Listing 5.4 Displaying Multiple DataGrid s on One Web Form
 [VB] 01: <%@ Page Language="VB" %> 02: <%@ Import Namespace="System.Data" %> 03: <%@ Import Namespace="System.Data.SqlClient" %> 04: 05: <script runat="server"> 06:   Sub Page_Load(Sender As Object, E As EventArgs) 07:     Dim myDataSet As New DataSet 08:     Dim myDataAdapter As SqlDataAdapter 09: 10:     myDataAdapter = New SqlDataAdapter("SELECT TOP 5 * FROM Customers", graphics/ccc.gif "server=localhost;database=Northwind;uid=sa;pwd=;") 11:     myDataAdapter.Fill(myDataSet, "Customers") 12: 13:     myDataAdapter.SelectCommand.CommandText = "SELECT TOP 5 * FROM Products" 14:     myDataAdapter.Fill(myDataSet, "Products") 15: 16:     myDataGrid.DataSource = myDataSet.Tables("Customers") 17:     myOtherDataGrid.DataSource = myDataSet.Tables("Products") 18:     Page.DataBind() 19:   End Sub 20: </script> [C#] 01: <%@ Page Language="C#" %> 02: <%@ Import Namespace="System.Data" %> 03: <%@ Import Namespace="System.Data.SqlClient" %> 04: 05: <script runat="server"> 06:   void Page_Load(Object sender, EventArgs e){ 07:     DataSet myDataSet = new DataSet(); 08:     SqlDataAdapter myDataAdapter; 09: 10:     myDataAdapter = new SqlDataAdapter("SELECT TOP 5 * FROM Customers", graphics/ccc.gif "server=localhost;database=Northwind;uid=sa;pwd=;"); 11:     myDataAdapter.Fill(myDataSet, "Customers"); 12: 13:     myDataAdapter.SelectCommand.CommandText = "SELECT TOP 5 * FROM Products"; 14:     myDataAdapter.Fill(myDataSet, "Products"); 15: 16:     myDataGrid.DataSource = myDataSet.Tables["Customers"]; 17:     myOtherDataGrid.DataSource = myDataSet.Tables["Products"]; 18:     Page.DataBind(); 19:   } 20: </script> [VB & C#] 21: <html> 22: <head> 23: <title>Programming Datadriven Web Applications with ASP.NET - Chapter 5</title> 24: </head> 25: <body> 26:   <p><asp:DataGrid id="myDataGrid" runat="server" /></p> 27:   <p><asp:DataGrid id="myOtherDataGrid" runat="server" /></p> 28: </body> 29: </html> 

On lines 10 and 13, you add a TOP 5 argument to the SQL statement so it returns only five records in each DataTable . On line 27, you add another DataGrid named myOtherDataGrid to display the data from the Products DataTable . On line 17, you set the DataSource property of myOtherDataGrid to the Products table, the same way that you set the DataSource property of myDataGrid in Listing 5.3.

One change in Listing 5.4 is on line 18. Rather than calling the DataBind() method for each DataGrid , you call it just for the Page object, which binds any child control of the Page object that has its DataSource set. Figure 5.3 shows the rendered Web page with two DataGrid controls.

Figure 5.3. Multiple DataGrid s can be used to display the data from multiple DataTable s. The Page.DataBind() method can be used to bind all child controls on the Web Form.
graphics/05fig03.gif
only for RuBoard


Programming Data-Driven Web Applications with ASP. NET
Programming Data-Driven Web Applications with ASP.NET
ISBN: 0672321068
EAN: 2147483647
Year: 2000
Pages: 170

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net