Thursday, 27 June 2013

Binding Data to Controls

Binding Data to Controls
Simple data binding is the ability to bind a control to a single data element. When you need to display and manipulate more than one data element at a time,then this is called complex data binding.Complex data binding is the ability to bind a control to more than one data element and more than one record in a database.
Class Hierarchies Relevant to Data Binding

//Text box controls—Used to display, or accept as input, a single line of text. Can also support multilines.

System.Windows.Forms.TextBox class

System.Windows.Forms.DataGridTextBox subclass

System.Web.UI.WebControls.Textbox class

//List controls—Enable you to display a list of items to the user that the user can select by clicking.

System.Windows.Forms.ListControl class

System.Windows.Forms.ComboBox subclass

System.Windows.Forms.ListBox subclass

System.Web.UI.WebControls.ListControls class

System.Web.UI.WebControls.CheckBoxList subclass

System.Web.UI.WebControls.DropDownList subclass

System.Web.UI.WebControls.ListBox subclass

System.Web.UI.WebControls.RadioButtonList subclass

//DataGrid controls—Used to display data in a scrollable, tabular grid.

System.Windows.Forms.DataGrid class

System.Web.UI.WebControls.DataGrid class


Methods Binding
For ListBox controls, you can bind the control to aDataSet class using the DataSource and DisplayMemberproperties as you have seen in our Windows Forms example:

ListBox1.DataSource = customer
ListBox1.DisplayMember = "Customers.CompanyName"

Or, you can use the DataBinding method programmatically to achieve the same results.

ListBox1.DataBindings.Add(New System.Windows.Forms.Binding
("SelectedItem", customer, "Customers.CompanyName"))

For DataGrid controls, you bind the control to a DataSetclass using the DataSource and DataMember properties:
DataGrid1.DataMember = "Customers.CustomersOrders"
DataGrid1.DataSource = customer

Or, use the SetDataBinding method programmatically to achieve the same results:

DataGrid1.SetDataBinding(customer,"Customers.CustomersOrders")

No comments: