Types of Data Binding
Windows Forms can take advantage of two types of data binding: simple binding and complex binding. Each offers different advantages.
Simple data binding is the ability of a control to bind to a single data element, such as a value in a column in a dataset table or a property of another control. Therefore, it is typically used with TextBoxes and Labelscontrols that typically display only a single value. In fact, any property on a control can be bound to another property of a control.
Complex data binding is the ability of a control to bind to more than one data element. Controls that support complex binding include DataGrids, ListBoxes, and ErrorProviders.
In the Form example in Figure 1, I used simple data binding. I bound the Panel control to the enable property of CheckBox1. The visible property of the Panel and the checked property of the CheckBoxes are Boolean values, hence binding is achieved in a single statement. I shall later examine a complex scenario where both properties are not of the same type.
Me.Panel1.DataBindings.Add("Visible", CheckBox1, "Checked")
I added a data binding to the panel control via the DataBindings.Add method. The first parameter in the Add method specifies the Visible property of the Panel to which the binding needs to be added, the second property specifies the control (CheckBox1) to which to bind, and the last property specifies the property (Checked) of that control to bind to.
In the second line, I bound the enabled property of the Panel to the checked property of CheckBox2.
Me.Panel1.DataBindings.Add("Enabled", CheckBox2, "Checked")
What's important to note is that two properties of the Panel were bound to two different controls' properties.
Enhanced Data Binding
Let's enhance this scenario, From a TextBox, a user is allowed to set the background color of the Form, which changes dynamically (see Figure 2).
|Figure 2. Enhanced Data Binding: Here, the user can change the background color of the form by typing a string into a TextBox.|
To accomplish this you might be tempted to use the following statement:
Me.DataBindings.Add("BackColor", TextBox1, "Text")
However if you run the program with this line of code, you're going to get a casting error. Specifically "Invalid cast from System.String to System.Drawing.Color." The error occurs because you're trying to set the backcolor of the form to a text type whereas it's expecting a color type. Remember that the control property being bound and the property to which it is being bound must always be of the same type.
There are three simple steps to solve the problem of mismatched property types.
Step 1: Declare a class-level binding object with events.
Private WithEvents BackColorBinding As Binding 'Class level'
This code defines a binding object called "BackColorBinding." You need to place this code at the class level. Insert it after the comment "Declare BackColorBinding" in the file DataBound.vb.
Step 2: Create an instance of the binding object.
BackColorBinding = New Binding("BackColor", TextBox1, "Text")
This code creates a new instance of the binding object. You need to place it in the form load event. Insert it after the comment "Add code for BackColor Binding." Its parameters specify that you are binding the BackColor property of the form to the Text property of TextBox1. Further, you are adding this binding object (BackColorBinding) to the Data Bindings collection of the form. In this case the properties being bound are not the same type; one is color and the other text. Hence, we need one more step.
Step 3: Add code to the binding object's format event to return the required value.
The Binding.Format event occurs when the property of a control is bound to a data value. The format event occurs both when data is pushed from the data source into the control, and when the data is pulled from the control into the data source. In the former case, the binding uses the format event to put the formatted data into the control. It first parses the data using the parse event, and then formats it and pushes it into the control.
In this example, you don't need to use the parse event, but the information is useful in understanding the event mechanism. In our case, you need to convert the type from text to the requisite type as required by the BackColor property of the form. Add the following code to the format event of the BackColorBinding object, or just paste the code after the form load event.
Private Sub BackColorBinding_Format(ByVal sender As Object, ByVal e As _
System.Windows.Forms.ConvertEventArgs) Handles BackColorBinding.Format
If e.Value = "Red" Then
e.Value = Color.Red
If e.Value = "Green" Then
e.Value = Color.Green
e.Value = Color.Empty
If the user selects red or green as the back color, the ConvertEventArg is set to that value. Run the project to test the code.