ComboBox binding Dictionary as a data source

Original: the ComboBox binding Dictionary as a data source

The DataSource ComboBox may be set to: IList implementation object, such as a DataSet or an Array. The default is a null reference (in Visual Basic is Nothing).

But in the actual project you may need to bind Dictionary as a data source. At this point, the Dictionary <> BindingSource put in will be able to achieve indirect binding.

BindingSource component serves two purposes:

First, by providing a layer of indirection, the current item management, change notification, and other services to simplify the controls in a form of data binding. This is accomplished by attaching the BindingSource component to a data source, and then controls the form tie BindingSource component to achieve. All further interaction, including positioning, sorting, filtering, and updating of data, through implementation calls BindingSource component.

Second, BindingSource component strongly typed as a data source. Typically, the type of the underlying data source is fixed by one of the following mechanisms: an item may be added to the BindingSource component using the Add method. The DataSource property to a list, or a single object type. Both of these mechanisms create a strongly typed list. BindingSource support simple and complex data binding data by the DataSource and DataMember property indicates binding.

For more detailed information, please refer to BindingSource: http: //msdn.microsoft.com/zh-cn/library/system.windows.forms.bindingsource (VS.80) .aspx following sample code:

 Dictionary<string, string> dic = new Dictionary<string, string>();
 BindingSource bs = new BindingSource();
 bs.DataSource = dic;
 cbb.DataSource = bs;
 cbb.DisplayMember = "Value";
 cbb.ValueMember = "Key";

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/11746499.html
Recommended