Data Binding in .NET - C# Windows Forms - N
Data Binding in .NET - C# Windows Forms - N
Contents
Overview Binding Context CurrencyManager Bindable Data Sources Array or Collection ADO.NET Data Objects DataColumn DataTable DataView DataSet DataViewManager Example: Automatically synchroniz ed Master-Detail Managing Data Relation Binding to a DataGrid Binding to a Combobox Example: Form's BindingContext Position Management Handling of the PositionChanged event for the Form's BindingContext Format and Parse Events of the Binding Class Example: Binding to a ComboBox or ListBox Bind a ComboBox to an array of State objects
Overview
Data binding provides a way for developers to create a read/write link between the controls on a form and the data in their application (their data model). Classically, data binding was used within applications to take advantage of data stored in databases. Windows Forms data binding allows you to access data from databases as well as data in other structures, such as arrays and collections.
Binding Context
Each Windows Form has at least one BindingContext object that manages the CurrencyManager objects for the form. For each data source on a Windows Form, there is a single CurrencyManager object. Because there may be multiple data sources associated with a Windows Form, the BindingContext object enables you to retrieve any particular CurrencyManager object associated with a data source. Example For example if you add a TextBox control to a form and bind it to a column of a table (e.g. "Customers.FirstName") in a dataset (e.g. "dsCust"), the control communicates with the BindingContext object for that form. The BindingContext object, in turn, talks to the specific CurrencyManager object for that data association. If you queried the CurrencyManager's Position property, it would report the current record for that TextBox control's binding. In the example below, a TextBox control is bound to the FirstName column of a Customers table on the dsCust dataset through the BindingContext object for the form it is on. // Simple Data Binding txtBox.DataBindings.Add("Text",dsCust,"Customers.FirstName"); // Get current Rowposition CurrencyManager cm = (CurrencyManager)this.BindingContext[dsCust,"Customers"]; long rowPosition = (long)cm.Position;
1/8
CurrencyManager
The CurrencyManager is used to keep data-bound controls synchroniz ed with each other (showing data from the same record). The CurrencyManager object does this by managing a collection of the bound data supplied by a data source. For each data source associated with a Windows Form, the form maintains at least one CurrencyManager. Because there may be more than one data source associated with a form, the BindingContext object manages all of the CurrencyManager objects for any particular form. More broadly, all container controls have at least one BindingContext object to manage their CurrencyManagers. An important property of the CurrencyManager is the Position property. Currency is a term used to refer to the currentness of position within a data structure. You can use the Position property of the CurrencyManager class to determine the current position of all controls bound to the same CurrencyManager. For example, imagine a collection consisting of two columns called "ContactName" and "Phone". Two TextBox controls are bound to the same data source. When the Position property of the common CurrencyManager is set to the fourth position within that list (corresponding to the fifth name, because it is zero-based), both controls display the appropriate values (the fifth "ContactName" and the fifth "Phone") for that position in the data source. Example For example the Position property of the CurrencyManager is often manipulated in a Next / Prev Navigation Button.
// Position to next Record in Customer private void btnNext_Click(object sender, System.EventArgs e) { CurrencyManager cm = (CurrencyManager)this.BindingContext[dsCust,"Customers"]; if (cm.Position < cm.Count - 1) { cm.Position++; } }
2/8
Managing Data Relation One of the primary functions of a DataRelation is to allow navigation from one DataTable to another within a DataSet. This allows you to retrieve all the related DataRow objects in one DataTable when given a single DataRow from a related DataTable. For example, after establishing a DataRelation between a table of customers and a table of orders, you can retrieve all the order rows for a particular customer row using DataRow.GetChildRows. If you have two controls bound to the same datasource, and you do not want them to share the same position, then you must make sure that the BindingContext member of one control differs from the BindingContext member of the other control. If they have the same BindingContext, they will share the same position in the datasource. If you add a ComboBox and a DataGrid to a form, the default behavior is for the BindingContext member of each of the two controls to be set to the Form's BindingContext. Thus, the default behavior is for the DataGrid and ComboBox to share the same BindingContext, and hence the selection in the ComboBox is synchronized with the current row of the DataGrid. If you do not want this behavior, you should create a new BindingContext member for at least one of the controls. // Establish the Relationship "RelCustOrd" between Customers ---< Orders System.Data.DataRelation relCustOrd; System.Data.DataColumn colMaster1; System.Data.DataColumn colDetail1; colMaster1 = ds.Tables["Customers"].Columns["CustomerID"]; colDetail1 = ds.Tables["Orders"].Columns["CustomerID"]; relCustOrd = new System.Data.DataRelation("RelCustOrd",colMaster1,colDetail1); ds.Relations.Add(relCustOrd); Binding to a DataGrid This sample displays a Datagrid for the Orders and a DataGrid for the Order Details for each Order. The Customer Data is bound to a few Textboxes and a Combobox. We will use the Combo Box to select the company name. and display the contact name, phone number and fax number in the text boxes. As the selected customer changes, the DataGrid's updates to display the orders and order details for that customer. In order to link the two DataGrid objects, you need to set the DataSource of each DataGrid to the same DataSet . You also need to set the DataMember properties to indicate to the Windows Forms BindingContext that they are related. You do this by setting the DataMember for the DataGrid's to the name of the relationship between the Customers and Orders tables. The same is done for the Orders and Order Details Table. // Setup the grid's view and member data grdOrders.DataSource = dsView; grdOrders.DataMember = "Customers.RelCustOrd"; Binding to a Combobox The Combobox doesn't have a DataMember property - instead it has a DisplayMember and ValueMember property: The DisplayMember of a Combobox gets or sets a string that specifies the property of the data
3/8
source whose contents you want to display. The ValueMember property determines which value gets moved into the SelectedValue of the Combo Box. In the example, whenever the user selects a Customer by "Customers.CompanyName", the SelectedValue is the "Customers.CustomerID". Whenever the SelectedValue changes, the databinding moves the new value into the Customer object. // Setup the combobox view and display-, value member cbCust.DataSource = dsView; cbCust.DisplayMember = "Customers.CompanyName"; cbCust.ValueMember = "Customers.CustomerID"; C# Code using using using using using using using System; System.Drawing; System.Collections; System.ComponentModel; System.Windows.Forms; System.Data; System.Data.SqlClient;
namespace Akadia { // Shows Master-Detail, Table-Mapping, Fill a Combobox public class MasterDetail : System.Windows.Forms.Form { ..... // Fields private String ConnectionString; private DataViewManager dsView; private DataSet ds; public MasterDetail() { // Create Components InitializeComponent(); // Setup DB-Connection ConnectionString = "data source=xeon;uid=sa;password=manager;database=northwind"; SqlConnection cn = new SqlConnection(ConnectionString); // Create the DataSet ds = new DataSet("CustOrders"); // Fill the Dataset with Customers, map Default Tablename // "Table" to "Customers". SqlDataAdapter da1 = new SqlDataAdapter("SELECT * FROM Customers",cn); da1.TableMappings.Add("Table","Customers"); da1.Fill(ds); // Fill the Dataset with Orders, map Default Tablename // "Table" to "Orders". SqlDataAdapter da2 = new SqlDataAdapter("SELECT * FROM Orders",cn); da2.TableMappings.Add("Table","Orders"); da2.Fill(ds); // Fill the Dataset with [Order Details], map Default Tablename // "Table" to "OrderDetails". SqlDataAdapter da3 = new SqlDataAdapter("SELECT * FROM [Order Details]",cn); da3.TableMappings.Add("Table","OrderDetails"); da3.Fill(ds); // Show created Tablenames within the Dataset string myMessage = "Table Mappings: "; for(int i=0; i < ds.Tables.Count; i++) { myMessage += i.ToString() + " " + ds.Tables[i].ToString() + " "; } // Establish the Relationship "RelCustOrd" // between Customers ---< Orders System.Data.DataRelation relCustOrd; System.Data.DataColumn colMaster1; System.Data.DataColumn colDetail1; colMaster1 = ds.Tables["Customers"].Columns["CustomerID"]; colDetail1 = ds.Tables["Orders"].Columns["CustomerID"]; relCustOrd = new System.Data.DataRelation("RelCustOrd",colMaster1,colDetail1); ds.Relations.Add(relCustOrd); // Establish the Relationship "RelOrdDet" // between Orders ---< [Order Details] System.Data.DataRelation relOrdDet; System.Data.DataColumn colMaster2; System.Data.DataColumn colDetail2; colMaster2 = ds.Tables["Orders"].Columns["OrderID"]; colDetail2 = ds.Tables["OrderDetails"].Columns["OrderID"]; relOrdDet = new System.Data.DataRelation("RelOrdDet",colMaster2,colDetail2);
4/8
ds.Relations.Add(relOrdDet); // Show created Relations within the Dataset myMessage += "Relation Mappings: "; for(int i=0; i < ds.Relations.Count; i++) { myMessage += i.ToString() + " " + ds.Relations[i].ToString() + " "; } txtMessage.Text = myMessage; // The DataViewManager returned by the DefaultViewManager // property allows you to create custom settings for each // DataTable in the DataSet. dsView = ds.DefaultViewManager; // Grid Databinding grdOrders.DataSource = dsView; grdOrders.DataMember = "Customers.RelCustOrd"; grdOrderDetails.DataSource = dsView; grdOrderDetails.DataMember = "Customers.RelCustOrd.RelOrdDet"; // Combobox Databinding cbCust.DataSource = dsView; cbCust.DisplayMember = "Customers.CompanyName"; cbCust.ValueMember = "Customers.CustomerID"; // Text Columns Databinding txtContact.DataBindings.Add("Text",dsView,"Customers.ContactName"); txtPhoneNo.DataBindings.Add("Text",dsView,"Customers.Phone"); txtFaxNo.DataBindings.Add("Text",dsView,"Customers.Fax"); } // Position to prev Record in Customer private void btnPrev_Click(object sender, System.EventArgs e) { if (this.BindingContext[dsView,"Customers"].Position > 0) { this.BindingContext[dsView,"Customers"].Position--; } } // Position to next Record in Customer private void btnNext_Click(object sender, System.EventArgs e) { CurrencyManager cm = (CurrencyManager)this.BindingContext[dsView,"Customers"]; if (cm.Position < cm.Count - 1) { cm.Position++; } } .... .... // The main entry point for the application. static void Main() { Application.Run(new MasterDetail()); } } }
5/8
Handling of the PositionChanged event for the Form's BindingContext The BindingManagerBase.PositionChanged Event occurs when the Position property, managed by the CurrencyManager, changes. If you want to execute you own code, when this event is raised, then you can hook into the position changed event on the BindingContext. // Get the BindingManagerBase for the Customers List BindingManagerBase bmCustomers = this.BindingContext[custList]; // Add the delegate for the PositionChanged event. bmCustomers.PositionChanged += new EventHandler(customers_PositionChanged); // Position has changed - update the "Record X of N" Panel private void customers_PositionChanged(object sender, System.EventArgs e) { textBoxPosition.Text = String.Format ( "Record {0} of {1}", ( this.BindingContext[custList].Position + 1 ), custList.Count ); } Format and Parse Events of the Binding Class There are two events on the binding class that are most useful. They are Format and Parse. These two events are raised when ever the data is pushed from the data source to the control or when the data is pulled from the control to the data source. This allows you to do special validating and formatting of the data. The Format event is used for formatting the data from the data source before it is displayed on the control. So when data is pushed from the data source to the control in the Format event is raised and you can perform whatever data formatting or validation is necessary prior to displaying it. The Parse event is used when that data is changed in the control and needs to go back to the data source. A classic example of this process would be data that is stored as a decimal, but displayed as a currency. The code in the event handler for the Format event would take the decimal value and format it for currency display, while the code in the Parse event handler will take the currency and convert it back to decimal type. In our example, we want to format the DateOfBirth TextBox: // We want to format the DateOfBirth, so process the format and // parse events for the DateOfBirth text box. To accomplish this, // we add our own event handlers to the Format and Parse events. Binding dobBinding = new Binding("Text", custList, "DateOfBirth"); dobBinding.Format += new ConvertEventHandler(this.textBoxDOB_FormatDate); dobBinding.Parse += new ConvertEventHandler(this.textBoxDOB_ParseDate); textBoxDOB.DataBindings.Add(dobBinding); // Format the Date Field to short date form for display in the TextBox private void textBoxDOB_FormatDate(object sender, ConvertEventArgs e) { // We only deal with converting to dates and strings if (e.DesiredType != typeof(DateTime)) return; if (e.Value.GetType() != typeof(string)) return; string value = (string)e.Value; try { e.Value = DateTime.Parse(value); } catch(Exception ex)
6/8
{ MessageBox.Show(ex.Message); } } // Parse the textbox contents and turn them back into a date private void textBoxDOB_ParseDate(object sender, ConvertEventArgs e) { // We only deal with converting to strings from dates if (e.DesiredType != typeof(string)) return ; if (e.Value.GetType() != typeof(DateTime)) return ; DateTime dt = (DateTime)e.Value; e.Value = dt.ToShortDateString(); }
Bind a ComboBox to an array of State objects namespace Akadia.ComboBoxBinding { using using using using using using System; System.ComponentModel; System.Drawing; System.Windows.Forms; System.Data; System.Data.SqlClient;
public class ComboBoxBinding : System.Windows.Forms.Form { ..... // Lookup "Table" with States for the Comobox public struct State { private string _shortName, _longName; public State(string longName , string shortName) { this._shortName = shortName; this._longName = longName; } public string ShortName { get { return _shortName; } } public string LongName { get { return _longName; } } } // Define the Array of States for the DropDown List public State[] States = new State[] { new State("Alabama","AL") ,new State("Alaska","AK") ,new State("Arizona" ,"AZ") ,new State("Arkansas","AR") ,new State("California" ,"CA") ...... } ;
7/8
public ComboBoxBinding() { ....... // Populate the list comboBoxState.DataSource = States; // Define the field to be displayed comboBoxState.DisplayMember = "LongName"; // Define the field to be used as the value comboBoxState.ValueMember = "ShortName"; // Bind the selected value of the the ComboBox to the // Region field of the current Customer comboBoxState.DataBindings.Add("SelectedValue", customersDataSet1, "Customers.Region"); ........ } } }
8/8