Binding Data

Rather than iterating through the table and loading each record individually, you can bind the ListBox control to the database table. In general, data binding lets you create a relationship between a control (such as a ListBox) and a source of data (such as a DataSet whose backing store is SQL Server).

Virtually any object can act as a data source, including such simple sources as properties, expressions, or the result of a method call, and such complex sources as arrays, collections, and DataSets. For controls that display a collection, such as a listbox or a DataGrid, you must bind to a source that implements one of the binding interfaces such as ICollection, IBindingList, or IListSource. This allows the control to iterate over the collection and display each member in turn.

For example, you can modify the previous examples to remove the foreach loop:

figs/csharpicon.gif

// for each row in the table, display the info
foreach (DataRow dataRow in dataTable.Rows)
{
 lbBugs.Items.Add(
 dataRow["BugID"] + 
 ": " + dataRow["Description"] );
}

and replace it with these two statements:

figs/csharpicon.gif

lbBugs.DataSource= dataTable;
lbBugs.DisplayMember = "Description";

The first statement instructs the control to bind to a dataTable and populate itself based on that table's data. The second sets the DisplayMember property, which tells the ListBox which field within the table the ListBox will display.

The complete source code for the form constructor is shown in Example 19-8 for C# and Example 19-9 for VB.NET.

Example 19-8. Binding data in C#

figs/csharpicon.gif

public Form1( )
{
 //
 // Required for Windows Form Designer support
 //
 InitializeComponent( );
 
 // connect to the Bugs database
 string connectionString = 
 "server=YourServer; uid=sa; pwd=YourPassword; database=WindForm_Bugs";
 
 // get records from the Bugs table
 string commandString = 
 "Select BugID, Description from Bugs";
 
 // create the data set command object 
 // and the DataSet
 SqlDataAdapter dataAdapter = 
 new SqlDataAdapter(
 commandString, connectionString);
 
 DataSet DataSet = new DataSet( );
 
 // fill the data set object
 dataAdapter.Fill(DataSet,"Customers");
 
 // Get the one table from the DataSet
 DataTable dataTable = DataSet.Tables[0];
 
 // bind to the data table
 lbBugs.DataSource= dataTable;
 lbBugs.DisplayMember = "Description";
}

Example 19-9. Binding data in VB.NET

figs/vbicon.gif

Public Sub New( )
 MyBase.New( )
 
 'This call is required by the Windows Form Designer.
 InitializeComponent( )
 
 Dim connectionString As String
 connectionString = _
 "Server=YourServer; uid=sa; pwd=YourPassword; database=WindForm_Bugs"
 
 Dim commandString As String
 commandString = "Select BugID, Description from Bugs"
 
 Dim myDataAdapter As New System.Data.SqlClient.SqlDataAdapter( _
 commandString, connectionString)
 
 Dim myDataSet As New DataSet( )
 
 myDataAdapter.Fill(myDataSet, "Bugs")
 
 Dim myDataTable As DataTable
 myDataTable = myDataSet.Tables(0)
 
 ' bind to the data table
 lbBugs.DataSource = myDataTable
 lbBugs.DisplayMember = "Description"
 
End Sub




Programming. NET Windows Applications
Programming .Net Windows Applications
ISBN: 0596003218
EAN: 2147483647
Year: 2003
Pages: 148
Simiral book on Amazon

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