Complex Binding of Controls to Data

I l @ ve RuBoard

Simple binding is the process of tying a single control to a single data item. Complex binding ties a single control to many data items. Controls that are often used for complex data binding include DataGrid which has the appearance of a simple spreadsheet. This control can display, edit, or add and delete records from a table. A single databinding operation is used to attach the control to a DataTable . The DataGrid can only be used to show one table at a time, but it can be used to show relationships between tables.

Listing 3.3.3 shows a modified version of the simple binding example demonstrated in Listing 3.3.2. This listing removes all the controls associated with the DataSet and replaces them with a complex bound grid control. Don't be put off by the title. Complex data binding is complex only in its function, not its usage.

Listing 3.3.3 gridbind.cs : An Example of Complex Data Binding
 1: namespace Sams  2: {  3:   using System;  4:   using System.Drawing;  5:   using System.Collections;  6:   using System.Data;  7:   using System.ComponentModel;  8:   using System.Windows.Forms;  9: 10:   //This application shows simple data binding to a 11:   //programatically created dataset. 12:   public class GridBind : System.Windows.Forms.Form 13:   { 14:     private System.ComponentModel.Container components; 15:     private System.Windows.Forms.DataGrid dataGrid1; 16: 17:     //The dataset used to store the table 18:     private DataSet dataset; 19: 20:     //Called at creation to initialize the 21:     //dataset and create an empty table 22:     private void InitDataSet() 23:     { 24:       dataset = new DataSet("ContactData"); 25: 26:       DataTable t=new DataTable("Contacts"); 27: 28:       t.Columns.Add("First",typeof(System.String)); 29:       t.Columns.Add("Name",typeof(System.String)); 30:       t.Columns.Add("Company",typeof(System.String)); 31:       t.Columns.Add("Title",typeof(System.String)); 32:       t.Columns.Add("Phone",typeof(System.String)); 33: 34:       t.MinimumCapacity=100; 35: 36:       dataset.Tables.Add(t); 37:     } 38: 39:     //Called at initialization to do complex binding of the DataGrid 40:     //to the dataset's "Contacts" table entries 41:     private void BindGrid() 42:     { 43:       this.dataGrid1.SetDataBinding(dataset.Tables["Contacts"],""); 44:     } 45: 46:     //Constructor. Positions the form controls, 47:     //Ininitializes the dataset, binds the controls and 48:     //wires up the handlers. 49:     public GridBind() 50:     { 51:       InitializeComponent(); 52: 53:       InitDataSet(); 54: 55:       BindGrid(); 56: 57:     } 58: 59:     //Cleans up the Form 60:     public override void Dispose() 61:     { 62:       base.Dispose(); 63:       components.Dispose(); 64:     } 65: 66:     //Method added by the form designer 67:     private void InitializeComponent() 68:     { 69:       this.components = new System.ComponentModel.Container (); 70:       this.dataGrid1 = new System.Windows.Forms.DataGrid (); 71:       dataGrid1.BeginInit (); 72:       dataGrid1.Location = new System.Drawing.Point (8, 16); 73:       dataGrid1.Size = new System.Drawing.Size (472, 224); 74:       dataGrid1.DataMember = ""; 75:       dataGrid1.TabIndex = 0; 76:       this.Text = "GridBind"; 77:       this.AutoScaleBaseSize = new System.Drawing.Size (5, 13); 78:       this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D; 79:       this.ClientSize = new System.Drawing.Size (486, 251); 80:       this.Controls.Add (this.dataGrid1); 81:       dataGrid1.EndInit (); 82:     } 83: 84: 85: 86:     //Main method to instantiate and run the application. 87:     static void Main() 88:     { 89:       Application.Run(new GridBind()); 90:     } 91:   } 92: } 

Compile this program with the following command line:

 csc /t:winexe gridbind.cs 

Looking at the information in Listing 3.3.3, you can see that the code used to navigate the data set is no longer needed. The method to add a new row of data is not needed either. The DataGrid control does all that for you. Binding the data to the grid takes place on line 43. Initialization of the data set is identical to the previous example; it is shown on lines 22 “37. Figure 3.3.3 shows the GridBind application in action. Notice that the grid provides column and row headers that allow you to select a whole row or order the table according to column alphabetic order.

Figure 3.3.3. Complex binding to a DataGrid.

graphics/0303fig03.gif

The DataGrid control can also be used to show hierarchical relationships between multiple data tables. Perhaps a Customer Resource Management application would want to show the relationship between a customer and his or her orders or a client and his or her technical support incidents. Complex binding of data makes this otherwise difficult process painless.

So far, you have seen data binding to DataSets and DataTables created on-the-fly by the listing examples. A good real world example will be to show complex binding to an existing database.

The objects we have been using to store and manipulate the data are parts of the ADO.NET framework. This system allows high level interaction with database tables from many different database providers. ADO.NET can be used in conjunction with OleDB, SQL, and other database drivers. Take a look now at an example of complex bound database usage.

I l @ ve RuBoard


C# and the .NET Framework. The C++ Perspective
C# and the .NET Framework
ISBN: 067232153X
EAN: 2147483647
Year: 2001
Pages: 204

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