Visual C#

ADO.NET Visual Support: The Binding Source

Microsoft SQL Server does not provide user-friendly objects that a user can use to perform the necessary operations of a regular application. That is why…

2 min read373 words2 code samples

Introduction#

Since Microsoft SQL Server objects aren't designed for direct user interaction in standard applications, we use Microsoft Visual Studio to build interfaces with familiar Windows controls. These controls are general-purpose, so to display database values, you must link them to specific table columns. This linking is called binding, and it is handled by a binding source object.

Creating a Binding Source#

To support binding sources, the .NET Framework provides the BindingSource class from the System.Windows.Forms namespace. To visually create a binding source, from the Data section of the Toolbox, you can click BindingSource and click the form or container of your application. Because it is a non-visual object, its label would be positioned under the form. You can then specify its characteristics in the Properties window.

To programmatically create a binding source, you can declare a variable of type BindingSource. The class is equipped with three constructors. The default constructor allows you to simply declare the variable. Here is an example:

C#
private void btnBindingSource_Click(object sender, EventArgs e)
{
    BindingSource bsNames = new BindingSource();
}

The Data Source of a Binding Source#

When creating a binding source, you need to provide a name, which can be set in the Properties window if using the Toolbox. The core component of a binding source is its data source, the list containing the values. This can be almost any list type, such as an array, provided the class implements the IList interface. The BindingSource class uses a DataSource property of type object to handle this flexibility. To set this programmatically, create your list and assign it to the DataSource property of the BindingSource. Here is an example:

C#
private void btnBindingSource_Click(object sender, EventArgs e)
{
    BindingSource bsNames = new BindingSource();
    List<string> strNames = new List<string>();

    strNames.Add("Vicky Bisso");
    strNames.Add("Amy Warren");
    strNames.Add("Chrissie Childs");
    strNames.Add("Martial Otto");

    bsNames.DataSource = strNames;
}

To visually specify the data source of a binding source, access the Properties window of your BindingSource object. In the Properties window, click DataSource and click the arrow of its combo box:

  • If you had not previously created a data source, you can click Add Project Data Source... This would launch the Data Source Configuration Wizard that allows you to create and configure a data set
  • If you had previously created a data set object, you can expand the Other Data Sources node and expand the Project Data Sources node. You would see the created data set and you can select it. Here is an example:

Home Copyright © 2010-2011 C# Key