Data Binding

Data Binding

Earlier in this chapter, we demonstrated data binding using a Recordset (imported into a DataTable). However, that was only a small example of what is possible. This section looks at data binding in more detail, covering data binding in different environments, Web Forms controls vs. Windows Forms controls, and the kinds of objects that are supported for binding.

Binding to Windows Forms Controls

The .NET Framework brings a wide variety of controls to the developer. The Windows Forms controls are found in the System.Windows.Forms namespace, and the vast majority of them support data binding. In general, data binding to a control is far from challenging. It requires that you use an in-memory data store and can usually accept either a DataTable or a DataSet.

The mechanism is very simple. Take a look at the following example, using a DataTable:

Dim adapter As New OleDbDataAdapter("Select * From Customers", _  connStr) Dim dt As New DataTable() ' Import the query results into the DataTable adapter.Fill(dt) ' Bind the DataTable to the DataGrid DataGrid1.DataSource = dt

The DataSet works in almost exactly the same way:

Dim adapter As New OleDbDataAdapter("Select * From Customers", connStr) Dim ds As New DataSet() ' Import the query results into the DataSet adapter.Fill(ds) ' Bind a DataTable from the DataSet to the DataGrid DataGrid1.DataSource = ds.Tables("Customers")

Notice in this example that instead of passing the entire DataSet to the DataGrid, we ve chosen to bind only a single table. This is often necessary with controls for which multiple sets of tables don t make sense. The DataGrid is not one of these controls, however. It is capable of a multitable display (try it) and likes the DataSet just fine.

Binding with Web Forms Controls

Data binding with Web Forms controls requires an additional step. Web Forms controls reside under the System.Web.UI namespace, and because they are destined to run only within the ASP.NET world, some of their behaviors are, by necessity, different from those of their System.Windows.Forms counterparts. Data binding is just one example of such a behavior. The Windows Forms controls examples apply to Web Forms controls, but with a caveat: you need to explicitly tell the control to bind to the data source you specified, as follows:

Dim adapter As New OleDbDataAdapter("Select * From Customers", _  connStr) Dim ds As New DataSet() ' Import the query results into the DataSet adapter.Fill(ds) ' Bind a DataTable from the DataSet to the Web Form DataGrid DataGrid1.DataSource = ds.Tables("Customers") DataGrid1.DataBind()

Notice that this example is exactly the same as the Windows Forms DataSet example just given, with one additional line of code. Calling Data Bind on the Web Form DataGrid forces the binding to occur a necessary step, without which the control would not be bound to the specified data.



Upgrading Microsoft Visual Basic 6.0to Microsoft Visual Basic  .NET
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET w/accompanying CD-ROM
ISBN: 073561587X
EAN: 2147483647
Year: 2001
Pages: 179

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