Data Binding C# Help

The previous examples have used the DataGrid and DataGridView controls, which form only a small part of the controls in the .NET runtime that can be used to display data. The process of linking a control to a data source is called data binding.

In the Microsoft Foundation Class library, the process of linking data from class variables to a set of controls was termed Dialog Data Exchange (DDX), The facilities available within .NET for binding data to controls are substantially easier to use and also more capable, For example, in .NET you can bind data to most properties of a control, not just the text property, You can also bind data in a similar manner to ASP.NETcontrols.

Simple Binding

A control that supports single binding typically displays only a single value at once, such as a text box or radio button, The following example shows how to bind a column from a DataTable to a TextBox:

DataSet ds = CreateDataSet();
textBox.DataBindings.Add(“Text”, ds , “Products.ProductName”);

After retrieving some data from the Products table and storing it in the returned DataSet with the Create DataSet () method as shown here, the second line binds the Text property of the control (text.Box1) to the Products. ProductNarne column, Figure 32-13 shows the result of this type of data binding.

Figure 32-13

Figure 32-13

The text box displays a string from the database. Figure 32-14 shows the SQl. Server Management Studio tool could be used to verify the contents of the Products table to check that it is the right column and value.

Figure 32-14

Figure 32-14

Having a single text box onscreen with no way to scroll to the next or the previous record and no way to update the database is not very useful.

The following section shows a more realistic example and introduces the other objects that are necessary for data binding to work.

Data-Binding Objects

Figure 32-15 shows a class hierarchy for the objects that are used in data binding. This section discusses the BindingContext, CurrencyManager, and PropertyManager classes of the System Windows forms namespace and shows how they interact when data is bound to one or more controls on a form The shaded objects are those used in binding.

In the previous example, the DataBindings property of the TextBox control was used to bind a column from a DataSet, to the Text property of the control. The DataBindings property is an instance of the controlBindingsCollection shown in Figure 32-15:

textBox l DataBindings. Add (‘Text’, ds , ‘Products. ProductName’) ;

his line adds a Binding object to the ControlBindingsCollection.

Figure 32-15

Figure 32-15

BindingContext

Each Windows Form has a BindingContext property Incidentally, Form is derived from Control, which is where this property is actually defined, so most controls have this property, A BindingContext object has a collection of BindingManagerBase instances (see Figure 32-16).These instances are created and added to the binding manager object when a control is data-bound.

The BindingContext might contain several data sources, wrapped in either a CurrencyManager or a PropertyManager. The decision of which class is used is based on the data source itself.

If the data source, contains a list of items, such as a DataTable, DataView, or any object that implements the IList interface, a CurrencyManager will be used, A currencyManager can maintain the current position within that data source If the data source returns only a single value, a PropertyManager will be stored within the BindingContext.

Figure 32-16

Figure 32-16

A CurrencyManager or PropertyManager is created only once for a givendata source, If two text boxes are bound to a row from a DataTable, only one CurrencyManager will be created within the binding context.

Each control added to a form is linked to the form’s binding manager, so all controls share the same instance, When a control is initially created, its BindingContext property is null, When the control is added to the Controls collection of the form, the BindingContext is set to that of the form.

To bind a control to a form, an entry needs to be added to its DataBincings property, which is an instance of ControlBindingsCollection. The following code creates a new binding:

text.Box. DataBindings. Add ( ‘Text’, ds, Products. ProductName’ ) ;

Internally, the Add () method of ControlBindingsCollection creates a new instance of a Binding object from the parameters passed to this method and adds this to the bindings collection represented in Figure 32-17.

Figure 32-17 illustrates roughly what is going on when a Binding object is added to a Control, The binding links the control to a data source, which is maintained within the BindingContext of the Form (or control itself), Changes within the data source are reflected into the control, as are changes in the control.

Figure 32-17

Figure 32-17

Binding

This class links a property of the control to a member of the data source. When that member changes, the control’s property is updated to reflect this change, The opposite is also true – if the text in the text box is updated, this change is reflected in the data source.

Bindings can be set up from any column to any property of the control, For example, you can bind not only the text of a text box but also the color of that text box. It is possible to bind properties of a control to completely different data sources; for example, the color of the cell might be defined in a colors table and the actual data might be defined in another table.

CurrencyManager and PropertyManager

When a Binding object is created, a corresponding CurrencyManager or PropertyManager object is also created, provided that this is the first time that data from the given source has been bound, The purpose of this class is to define the position of the current record within the data source and to coordinate all list bindings when the current record is changed. Figure 32-18 displays two fields from the Products table and includes a way to move between records by means of a TrackBar control.

Figure 32-18

Figure 32-18

The following example shows the main ScrollingDataBinding code:

The scrolling mechanism is provided by the trackBar Scroll event handler, which sets the position of the Binding Context to the current position of the track bar thumb, Altering the binding context here updates the data displayed on the screen.

Data is bound to the to text boxes in the retrieve Button_Click event by adding a data binding expression, Here the Text properties of the controls are set to fields from the data source, It is possible to bind any simple property of a control to an item from the data source; for example, you could bind the text color enabled, or other properties as appropriate.

When the data is originally retrieved, the maximum position on the track bar is set to be the number of records Then, in the scroll method, the position of the BindingContext for the products DataTable is set to the position of the scroll bar thumb. This changes the current record from the DataTable, so all controls bound to the current row (in this example, the two text boxes) are updated.

Now that you know how to bind to various data sources, such as arrays, data tables, data views, and various other containers of data, and how to sort and filter that data, the next section discusses how Visual Studio has been extended to permit data access to be better integrated with the application.

Posted on November 2, 2015 in Data Binding

Share the Story

Back to Top