ComboBox binding dictionary as a data source

Source: Internet
Author: User

Http://www.cnblogs.com/refresh/archive/2012/07/14/2591503.html

Https://msdn.microsoft.com/zh-cn/library/system.windows.forms.bindingsource (v=vs.80). aspx

The value of the DataSource of a ComboBox can be set to an object that implements the IList interface, such as a DataSet or Array. The default is a null reference (Nothing in Visual Basic).

However, you may need to bind dictionary as the data source in the actual project. In this case, you can implement the indirect binding by placing the dictionary<> in the BindingSource.

BindingSource components are used in two ways:

First, it simplifies the binding of controls to data in a form by providing an indirection layer, current item management, change notification, and other services. This is done by attaching the BindingSource component to the data source, and then tying the controls in the form to the BindingSource component. All further interactions with the data, including positioning, sorting, filtering, and updating, are implemented by calling the BindingSource component.

Second, the BindingSource component can act as a strongly typed data source. Typically, the type of the underlying data source is fixed by one of the following mechanisms: Use the Add method to add an item to the BindingSource component. Set the DataSource property to a list, a single object, or a type. Both of these mechanisms create a strongly typed list. BindingSource supports simple data binding and complex data binding as indicated by its DataSource and DataMember properties.

For more detailed information on BindingSource, refer to: Http://msdn.microsoft.com/zh-cn/library/system.windows.forms.bindingsource (vs.80). The ASPX sample code is as follows:

1 dictionary<string, string> dic = new dictionary<string, string> (); 2 BindingSource bs = new BindingSource (); 3 Bs. DataSource = Dic;4 CBB. DataSource = Bs;5 CBB. DisplayMember = "Value"; 6 CBB. ValueMember = "Key";

ComboBox binding dictionary as a data source

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.