Windows Forms Data Binding

One of the most powerful aspects of .NET and Windows Forms is data binding. Historically, data binding was used to bind views to data stored in databases. Some database management systems, such as Microsoft Access, have provided GUI APIs to help developers quickly bind to data.

 

This sample is taken from Chapter 6: "Windows Forms Data Binding" of the Blue Vision Title "Developing .NET Custom Controls and Designers using C#"


Advanced Data Binding

As we know from the previous sections, all controls on a form will contain a DataBindings property. Here is a view of the PropertyGrid for a TextBox, showing this property:

By default, the Text and Tag property are shown when expanding the DataBindings property. The Tag property of a control is used to provide custom data associated with the control. You may add additional properties to this expanded list by choosing them from the Advanced Data Binding s dialog box, which is accessed by clicking the ellipsis next to "Advanced." This dialog is shown here:

In order for Advanced Data Binding  to work, your form must contain a design-time data source component. You may provide a design-time data source by dragging a DataSet from the toolbox.

Once you have a data source component, you simply associate each control you want bound in the "Advanced Data Binding" dialog with the data source component. As you associate each property with the data source, the property will be added along with the Text and Tag property beneath (DataBindings) in the PropertyGrid .

When using Advanced Data Binding , you must be sure that properties are not bound twice. If you use the Advanced Data Binding dialog to bind a control's property, and then use the control's DataBindings property programmatically to bind the same property, a runtime error will occur.

Dynamic Properties

By default, any properties set on a control in the designer are persisted either in code or in a resource file. If the Localization property of the parent form is set to true, a control's properties are persisted in a resource file. Otherwise, they are persisted in code.

There may be situations, however, where certain properties should be customized by the user or some other customization application. These scenarios include customizing the BackColor of a form, or the FlatStyle property of a button. In situations like these, it is common to implement configuration files. And with .NET, this capability is built in.

Every Windows Forms application will expect to read from configuration files using a predetermined naming convention. The format is MyApp.exe.config. For example, if your application is named MyApp.exe, then your configuration file should be named MyApp.exe.config, and it should be placed in the same directory as the application itself. Note that you should not add the MyApp.exe.config file to the project, because it may be regenerated. If you need a configuration file to use during development, then the file should be app.config. Set the Build Action for this file to None. Compilation uses this file to regenerate MyApp.exe.config in the appropriate runtime directory.

Every Windows Forms control has a design-time property named "DynamicProperties." This property is a collection of name-value pairs which map key names to property names. These key names are stored in the application's configuration file. Property values can then be persisted in this configuration file and retrieved during a form's initialization. In code, each property will be associated with a key name. This key name is then used to read the property's value. Here are snapshots of the DynamicProperties section in the PropertyGrid and the DynamicProperties dialog for the TextBox.Text property:



As more properties are selected, which is indicated by a check box, these properties will be expanded beneath DynamicProperties in the PropertyGrid . The Key mapping on the right contains a list of all keys that have been added, and which can be cast to that property's type. In other words, you can specify more than one property to use the same key, but the value of that key must be able to be cast to the types of both properties; otherwise it wont appear in the list. This is determined by the first property that is configured. For example, the ReadOnly property will have an available key mapping of all Boolean properties that have been configured; the Text property will contain a key mapping of all Boolean properties and all String properties that have been configured.

Summary

In this chapter we looked at the concepts and the architecture behind .NET data binding. We showed the relationship between controls and the BindingContext, and covered the interfaces related to data binding. We also walked through examples of the two types of data binding: simple binding and complex binding.

Simple binding involves binding a single property of a control to a single column or property. Complex binding involves viewing a collection of objects and properties or rows and columns. Most importantly, we learned that the synchronization of a data source with its controls is loosely coupled through the CurrencyManager . Data no longer remembers its current position. This allows for multiple bindings and synchronization on the same data.

Lastly, we discussed dynamic properties, their use, and how they are persisted in application configuration files. This concept allows the user to be in more control over the user interface, if desired.

You should now have a better understanding of data binding in .NET Windows Forms. And remember, data no longer remembers its current position.



Comments

Be the first to write a comment

You must me logged in to write a comment.