Before getting started, let's take a look at the two forms of winform control data binding,Simple data binding and complex data binding.
1)Simple data binding
Simple data binding is to bind a property of a user control to a property of a Type instance. BIND as follows: Reference control. databindings. Add ("control property", instance object, "property name", true );
2)Complex Data Binding
Complex data binding involvesList-basedUser Controls (such as ComboBox, ListBox, errorprovider, and datagridview) bound to oneList of Data Objects.
Basically, complex data binding in Windows Forms allows binding to a list of data that supports the ilist interface. To bind a bindingsource component, You can bind it to a data list that supports the ienumerable interface.
For complex data binding, common data source types include (CodeComboBox is used as an example control ):
1) IlistInterface (including one-dimensional array, arraylist)
Example:
Private void initialcomboboxbylist ()
{
Arraylist list = new arraylist ();
For (INT I = 0; I <10; I ++)
{
List. Add (New dictionaryentry (I. tostring (), I. tostring () + "_ value "));
}
This. combobox1.displaymember = "key ";
This. combobox1.valuemember = "value ";
This. combobox1.datasource = List;
}
2)IlistsourceInterface (Datatable,Dataset)
Private void initialcomboboxbydatatable ()
{
Datatable dt = new datatable ();
Datacolumn DC1 = new datacolumn ("name ");
Datacolumn DC2 = new datacolumn ("value ");
DT. Columns. Add (DC1 );
DT. Columns. Add (DC2 );
For (INT I = 1; I <= 10; I ++)
{
Datarow DR = DT. newrow ();
Dr [0] = I;
Dr [1] = I + "--" + "hello ";
DT. Rows. Add (DR );
}
This. combobox1.displaymember = "name ";
This. combobox1.valuemember = "value ";
This. combobox1.datasource = DT;
}
3)IbindinglistInterface (suchBindinglistClass)
4)IbindinglistviewInterface (suchBindingsourceClass)
Example:
Private void initialcomboboxbybindingsource ()
{
Dictionary <string, string> DIC = new bindingsource <string, string> ();
For (INT I = 0; I <10; I ++)
{
Dic. Add (I. tostring (), I. tostring () + "_ Hello ");
This. combobox1.displaymember = "key ";
This. combobox1.valuemember = "value ";
This. combobox1.datasource = new bindingsource (DIC, null );
}
}
Note the aboveProgramThe datasource attribute value of bindingsource is a dictionary instance. If you use DIC to assign a value to ComboBox directly, the program reports an error because dictionary <> does not implement the ilist or ilistsource interface, however, you can bind a dictionary to bindingsource. For more details about bindingsource, refer to the following link:
Http://msdn.microsoft.com/zh-cn/library/system.windows.forms.bindingsource (vs.80). aspx
Let's look at the following bindingsource example:
Private void initialcomboboxbyobject ()
{
This. combobox1.displaymember = "name ";
This. combobox1.valuemember = "value ";
This. combobox1.datasource = new bindingsource (New dictionaryentry ("test", "test_value"), null );
}
In the code above, the datasource of bindingsource is a schema type dictionaryentry. Similarly, the dictionaryentry cannot be directly assigned to the datasource of ComboBox, but it can still be indirectly implemented through bindingsource.
This is because:
BindingsourceIt can be used as a strongly typed data source. The data source type is fixed by one of the following mechanisms:
· Use the add method to add an itemBindingsourceComponent.
· Set the datasource attribute toList,Single ObjectOrType. (These three do not have to implement ilist or ilistsource)
both of these mechanisms create a list of strong types. bindingsource supports simple data binding and complex data binding indicated by its datasource and datamember attributes.