Use Data Binding in C# 5

Data Binding is indispensable for a programmer. It allows data(or a group) to change when it is bound to another data item. The Binding concept uses the fact that attributes in a table have some relation to each other. When the value of one field changes, the changes should be effectively seen in the other fields. This is similar to the Look-up function in Microsoft Excel. Imagine having multiple text boxes whose value depend on a key field. This key field may be present in another text box. Now, if a value in the Key field changes, the change must be reflected in all other text boxes.

In C Sharp(Dot Net), combo boxes can be used to place key field/s. Working with combo boxes is much easier compared to text boxes. We can easily bind field/s in a data table created in SQL by merely setting some properties in a combo box. Combo box has three main fields that have to be set to effectively add contents of a data field(Column) to the domain of values in the combo box. We shall also learn how to bind data to text boxes from a data source.
  • First set the Data Source property to the existing data source which could be a dynamically created data table or could be a link to an existing SQL table as we shall see.

  • Set the Display Member property to the column that you want to display from the table.
  • Set the Value Member property to the column that you want to choose the value from.
 Consider a table shown below:

The Item Number is the key and the Item Value DEPENDS on it. The aim of this program is to create a DataTable during run-time & display the columns in two combo boxes.The following example shows a two-way dependency i.e.  if the value of any combo box changes, the change must be reflected in the other combo box. Two-way updates the target property or the source property whenever either the target property or the source property changes.The source property changes first then triggers an update in the Target property. In Two-way updates either field may act as a Trigger or a Target. To illustrate this, we simply write the code in the Load event of the form. The code is shown below:

namespace Use_Data_Binding
    public partial class Form1 : Form
        public Form1()

        private void Form1_Load(object sender, EventArgs e)
            //Create The Data Table
            DataTable dt = new DataTable();

            //Set Columns
            dt.Columns.Add("Item Number");
            dt.Columns.Add("Item Name");

            //Add Rows/Records
            dt.Rows.Add("1", "T.V.");
            dt.Rows.Add("4", "Laptop");
            dt.Rows.Add("5", "Speakers");

            //Set Data Source Property
            comboBox1.DataSource = dt;
            comboBox2.DataSource = dt;

            //Set Combobox 1 Properties
            comboBox1.ValueMember = "Item Number";
            comboBox1.DisplayMember = "Item Number";

            //Set Combobox 2 Properties
            comboBox2.ValueMember = "Item Number";
            comboBox2.DisplayMember = "Item Name";

Custom Search