Formatting and Parsing Bound Values
Sometimes the way the data is stored in the data source is not exactly how you want to display it. For example, you may want to format a date-time string, or take a raw image stored in the database and turn it into a Bitmap object for binding against a PictureBox control.
The way you dealt with these kinds of situations in the past was to handle the Format and Parse events on the Binding object for simple bound controls, or to handle control-level events for DataGrid or ComboBox controls to intercept the data as it was being bound. You can still use those events for specialized situations, but in .NET 2.0, things have become a lot easier because there is built-in support in the Binding class for doing automatic formatting and parsing of values based on type converters associated with the data value types being used.
|In .NET 2.0, the data binding capabilities of forms and controls has been redesigned in a way that makes it much easier to keep complex data collections synchronized and formatted as desired.|
The Binding class now has a number of new overloads and properties that let you influence automatic formatting that is done by the class as it grabs values out of the data source and before it sets the corresponding property on the bound control. You can set format strings or provide your own format provider. You can also influence when the formatting occurs. Parsing follows a reverse process and just works in many common cases.
As a simple example, say you wanted to enhance the master-details form presented before to include a textbox that displays the publication date of whichever row in the titles grid is currently selected, and you want to display the date in a MM/YYYY format. All you need to do to support this is set a few extra pieces of information on the Binding object before you add it to the DataBindings
collection of the TextBox:
Binding pubBinding = new Binding("Text",
m_TitlesBindingSource, "pubdate", true);
pubBinding.FormatString = "MM/yyyy";
pubBinding.NullValue = "<null>";
The fourth argument to the Binding constructor turns automatic formatting on. You can then provide a format string if the type being formatted (DateTime in this case) has a default formatter that will know what to do with the string you provide. If not, there is a FormatProvider
property that you can set to provide a specialized formatter. As you can see, you can also specify what value should be displayed for DBNull values, and if that same value is read back in while parsing, a DBNull will be placed in the underlying data source.
The automatic formatting will try to find a type converter to use if the bound property is not a string. For example, if the bound property type is Image, and the value in the data source is a byte array, there is a default type converter for image that takes the byte array and tries to convert it to an Image instance by serializing the bytes back in.
You can do a lot more through the synchronization mechanisms of BindingSource and Binding objects, and through the formatting capabilities of Binding objects. In Windows Forms 2.0, you should never need to dive down and directly work with the BindingContext of the form or its CurrencyManager and PropertyManager objects as was necessary in .NET 1.1. These details are all now neatly encapsulated for you in the BindingSource and Binding objects. Most of the time for simple situations you can use the designers to write the code for you through simple drag and drop operations. But when you need to handle more complex situations, you will have to write the code yourself, and hopefully this article has given you a little more insight into how things are working and how to control it yourself.