Simple Binding to a DataSet

I l @ ve RuBoard

Binding of all sorts of controls is an important feature of .NET. Databound controls are available in Windows Forms, WebForms, and ASP.NET. All these controls can use ADO.NET to bind to database tables either on the local machine or somewhere else on the network. The vehicle for database binding is the DataSet . This class represents a set of cached data that is held in memory. The data cache can be created manually or populated with data drawn from a database using ADO.NET's database connection system. After the information is in that memory store on your machine, you can bind controls to individual columns of data and select rows for editing or viewing.

The easiest way to understand a DataSet is to use it. Listing 3.3.2 shows an application that generates and manipulates a simple DataSet on your machine using simple binding to controls.

Listing 3.3.2 datasetapp.cs : Simple Binding of Multiple Controls
 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 datasetapp : System.Windows.Forms.Form  13:     {  14:         private System.ComponentModel.Container components;  15:  16:         //Component declarations  17:         private Label lbl_first,lbl_name,lbl_title,lbl_company,lbl_phone;  18:         private TextBox FirstName,SurName,Title,Company,Phone;  19:         private Button btnNext, btnPrev, btnNew, btnEnd;  20:  21:         //The dataset used to store the table  22:         private DataSet dataset;  23:  24:         //Button handler to navigate backwards through the table records  25:         private void OnPrev(Object sender,EventArgs e)  26:         {  27:            this.BindingContext[dataset.Tables["Contacts"]].Position--;  28:         }  29:  30:         //Button handler to navigate forward through the table records  31:         private void OnNext(Object sender,EventArgs e)  32:         {  33:            this.BindingContext[dataset.Tables["Contacts"]].Position++;  34:         }  35:  36:         //Button handler to create a new row  37:         private void OnNew(Object sender, EventArgs e)  38:         {  39:             NewEntry();  40:         }  41:  42:     //Button handler to exit the application  43:     private void OnEnd(Object sender, EventArgs e)  44:     {  45:         Application.Exit();  46:     }  47:  48:         //Method to move to the last record. Used when adding a row.  49:         private void MoveToEnd()  50:         {  51:           this.BindingContext[dataset.Tables["Contacts"]].Position=  52:             dataset.Tables["Contacts"].Rows.Count-1;  53:         }  54:  55:         //Method to add a new row to the table. Called at initialization  56:         //and by the "New" button handler.  57:         private void NewEntry()  58:         {  59:             DataRow row = dataset.Tables["Contacts"].NewRow();  60:             //set up row data with new entries of your choice  61:             row["First"]="Blank";  62:             row["Name"]="";  63:             row["Company"]="";  64:             row["Title"]="";  65:             row["Phone"]="";  66:             dataset.Tables[0].Rows.Add(row);  67:             dataset.AcceptChanges();  68:             MoveToEnd();  69:         }  70:  71:         //Called at creation to initialize the  72:         //dataset and create an empty table  73:         private void InitDataSet()  74:         {  75:             dataset = new DataSet("ContactData");  76:  77:             DataTable t=new DataTable("Contacts");  78:  79:             t.Columns.Add("First",typeof(System.String));  80:             t.Columns.Add("Name",typeof(System.String));  81:             t.Columns.Add("Company",typeof(System.String));  82:             t.Columns.Add("Title",typeof(System.String));  83:             t.Columns.Add("Phone",typeof(System.String));  84:  85:             t.MinimumCapacity=100;  86:  87:             dataset.Tables.Add(t);  88:         }  89:  90:         //Called at initialization to do simple binding of the edit  91:         //controls on the form to the dataset's "Contacts" table entries  92:         private void BindControls()  93:         {  94:             FirstName.DataBindings.Add("Text",dataset.Tables["Contacts"], "First");  95:             SurName.DataBindings.Add("Text",dataset.Tables["Contacts"], "Name");  96:             Title.DataBindings.Add("Text",dataset.Tables["Contacts"], "Title");  97:             Company.DataBindings.Add("Text",dataset.Tables["Contacts"], "Company");  98:             Phone.DataBindings.Add("Text",dataset.Tables["Contacts"], "Phone");  99:         } 100: 101:         //Constructor. Positions the form controls, 102:         //Ininitializes the dataset, binds the controls and 103:         //wires up the handlers. 104:         public datasetapp() 105:         { 106:             this.components = new System.ComponentModel.Container (); 107:             this.Text = "datasetapp"; 108:             this.AutoScaleBaseSize = new System.Drawing.Size (5, 13); 109:             this.ClientSize = new System.Drawing.Size (250, 200); 110:             this.FormBorderStyle = FormBorderStyle.Fixed3D; 111: 112:             lbl_first = new Label(); 113:             lbl_first.Text="First name"; 114:             lbl_first.Location = new Point(5,5); 115:             lbl_first.Size = new Size(120,28); 116:             lbl_first.Anchor = AnchorStyles.Left  AnchorStyles.Right; 117:             Controls.Add(lbl_first); 118: 119:             FirstName = new TextBox(); 120:             FirstName.Location = new Point(125,5); 121:             FirstName.Size = new Size(120,28); 122:             FirstName.Anchor = AnchorStyles.Left  AnchorStyles.Right; 123:             Controls.Add(FirstName); 124: 125:             lbl_name = new Label(); 126:             lbl_name.Text="Surname"; 127:             lbl_name.Location = new Point(5,35); 128:             lbl_name.Size = new Size(120,28); 129:             lbl_name.Anchor = AnchorStyles.LeftAnchorStyles.Right; 130:             Controls.Add(lbl_name); 131: 132:             SurName = new TextBox(); 133:             SurName.Location = new Point(125,35); 134:             SurName.Size = new Size(120,28); 135:             SurName.Anchor = AnchorStyles.Left  AnchorStyles.Right; 136:             Controls.Add(SurName); 137: 138:             lbl_company = new Label(); 139:             lbl_company.Text="Company"; 140:             lbl_company.Location = new Point(5,65); 141:             lbl_company.Size = new Size(120,28); 142:             Controls.Add(lbl_company); 143: 144:             Company = new TextBox(); 145:             Company.Location = new Point(125,65); 146:             Company.Size = new Size(120,28); 147:             Controls.Add(Company); 148: 149:             lbl_title = new Label(); 150:             lbl_title.Text="Title"; 151:             lbl_title.Location = new Point(5,95); 152:             lbl_title.Size = new Size(120,28); 153:             Controls.Add(lbl_title); 154: 155:             Title = new TextBox(); 156:             Title.Location = new Point(125,95); 157:             Title.Size = new Size(120,28); 158:             Controls.Add(Title); 159: 160:             lbl_phone = new Label(); 161:             lbl_phone.Text="Telephone"; 162:             lbl_phone.Location = new Point(5,125); 163:             lbl_phone.Size = new Size(120,28); 164:             Controls.Add(lbl_phone); 165: 166:             Phone = new TextBox(); 167:             Phone.Location = new Point(125,125); 168:             Phone.Size = new Size(120,28); 169:             Controls.Add(Phone); 170: 171:             btnNew = new Button(); 172:             btnNew.Location = new Point(5,155); 173:             btnNew.Size = new Size(70,28); 174:             btnNew.Text="New"; 175:             btnNew.Click+=new EventHandler(OnNew); 176:             Controls.Add(btnNew); 177: 178:             btnPrev = new Button(); 179:             btnPrev.Location = new Point(80,155); 180:             btnPrev.Size = new Size(35,28); 181:             btnPrev.Text="<<"; 182:             btnPrev.Click += new EventHandler(OnPrev); 183:             Controls.Add(btnPrev); 184: 185:             btnEnd = new Button(); 186:             btnEnd.Location = new Point(120,155); 187:             btnEnd.Size = new Size(70,28); 188:             btnEnd.Text="End"; 189:             btnEnd.Click += new EventHandler(OnEnd); 190:             Controls.Add(btnEnd); 191: 192:             btnNext = new Button(); 193:             btnNext.Location = new Point(200,155); 194:             btnNext.Size = new Size(35,28); 195:             btnNext.Text=">>"; 196:             btnNext.Click += new EventHandler(OnNext); 197:             Controls.Add(btnNext); 198: 199:             InitDataSet(); 200: 201:             NewEntry(); 202: 203:             BindControls(); 204: 205:         } 206: 207:         //Cleans up the Form 208:         public override void Dispose() 209:         { 210:             base.Dispose(); 211:             components.Dispose(); 212:         } 213: 214:         //Main method to instantiate and run the application. 215:         static void Main() 216:         { 217:             Application.Run(new datasetapp()); 218:         } 219:     } 220: } 

Let's examine Listing 3.3.2 in more detail. The main initialization of the DataSet takes place on lines 73 “88. Here, a DataSet is created and a new table placed in its tables collection. The table is given a set of columns that are identified by the strings "First" , "Name" , and so on.

Adding data to a table is done one row at a time. The method on lines 57 “69 adds a new row to the end of the table and optionally fills it with text. You could also create a table with numeric values or other types.

Lines 92 “99 do the binding of the data in the table to the Text property of the identified controls. These controls can now be used to edit the information in the table, and they will display each entry automatically as you navigate through the database.

The button handlers that do the navigation are on lines 24 “37. These use the ContextManager.Position property to index the rows in the table.

Figure 3.3.2 shows a screenshot of the application.

Figure 3.3.2. The DataSet simple binding application.

graphics/0303fig02.gif

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