Special Binding and Formatting
Simple data binding between a control property and a data field is implemented through a Binding object. As mentioned, the binding class defines a logical link between a pair of names. Live objects are attached to both names only when the Binding object is added to the DataBindings collection of a physical control.
This said, how does the data binding manage this? More precisely, what if type and format of the input data (the data field) don't match the expectation of the control property? Let's review a first relatively simple case.
Suppose you have a TextBox control and a date field. You can create the Binding object as follows.
Dim b As Binding
b = New Binding("Text", dataSet, _
By default, the TextBox control displays a date string similar to the following:
What if you want to give it a different format? The Binding class provides a couple of events you can handle to pre- and post-process the data being displayed through the control. The preprocessing event is Format;
the post-processing event is Parse
The following code snippet shows how to write a Format
event handler to change the format of a date field.
Sub HireDate_Format(ByVal sender As Object, _
ByVal e As ConvertEventArgs) _
' Format the hire date into a more
' convenient output format
Dim dt As DateTime
dt = CType(e.Value, DateTime)
e.Value = dt.ToString("MMM dd, yyyy (ddd)")
The event handler must be a delegate of type ConvertEventHandler which carries over a parameter type of ConvertEventArgs. The structure has one key and read/write memberValue
When the event handler executes the Value
field contains the originally formatted data coming from the data source. You can cast or format it at will and you can store the modified value back into the Value
member. What you store in Value
will actually be passed to the bound control. In doing so, make sure the passed value has a type compatible with the bound control property. Do you want one more example illustrating just this point? Read on.
Suppose now you have to bind a BLOB field to a PictureBox control. The bindable property of the PictureBox control is Image
, which requires an object of type System.Drawing.Image. You cannot get an instance of this object out of any database (prior to SQL Server 2005).
In this case, the code for the Format
event is a bit more sophisticated, as Listing 3
The original data comes in as an array of bytes. Since you cannot bind an array of bytes to a PictureBox, you need to transform it into an Image type. Additionally, you cannot create an Image type out of a stream of bytes. That's why in Listing 3
I use an intermediate MemoryStream object to contain the bytes and use them to initialize a System.Drawing.Bitmap object that you can bind to a PictureBox control. (Note that Image is the base class of Bitmap.)
event requires the same delegate as the Format
event. The event is triggered before the data in the bound control saved to the in-memory data source. In this case, the expression e.Value
contains the client value that you can format or cast to match the type expected in the corresponding field of the data source.
Windows Forms data binding is a feature with many facets. You can associate data-bound expressions with properties of controls no matter the underlying type and you can include text and Image types. You also have events to control how the data is marshaled back and forth between the user interface and the back end memory.
The DataGrid, ListBox, and ComboBox controls allow you to associate an entire data source to their user interface. You can use any of the data sources that you can use in ASP.NET applications.
The data binding mechanism is ruled by a component known as the binding manager, which groups together all controls in a form that share the same data source object. To these control, the binding manager provides properties and events to make them capable of detecting each other's changes and selection changes. Thanks to this feature you can build master/details views as well as complex and inter-related user interfaces more quickly and efficiently. Your controls can auto-update their user interface reflecting changes in other controls. It's not magic; it's pure smart code.