zoukankan      html  css  js  c++  java
  • COMBOBOX绑定DICTIONARY做为数据源

    ComboBox的DataSource的值可设置为 :实现 IList 接口的对象,如 DataSet 或 Array。默认为 空引用(在 Visual Basic 中为 Nothing)。

    但是在实际的项目中可能需要绑定Dictionary作为数据源。此时,将Dictionary<>放到BindingSource中便可以实现间接绑定。

    BindingSource 组件有两种用途:

    首先,它通过提供一个间接寻址层、当前项管理、更改通知和其他服务简化了窗体中控件到数据的绑定。这是通过将 BindingSource 组件附加到数据源,然后将窗体中的控件绑BindingSource 组件来实现的。与数据的所有进一步交互,包括定位、排序、筛选和更新,都通过调用 BindingSource 组件实现。

    第二,BindingSource 组件可以作为一个强类型的数据源。通常,基础数据源的类型通过以下机制之一固定: 使用 Add 方法可将某项添加到 BindingSource 组件中。 将 DataSource 属性设置为一个列表、单个对象或类型。 这两种机制都创建一个强类型列表。BindingSource 支持由其 DataSource 和 DataMember 属性指示的简单数据绑定和复杂数据绑定。

    有关BindingSource的更详细信息请参考:http://msdn.microsoft.com/zh-cn/library/system.windows.forms.bindingsource(VS.80).aspx 示例代码如下:

    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";
  • 相关阅读:
    HDU 2844 Coins(多重背包)
    HDU 4540 威威猫系列故事——打地鼠(DP)
    Codeforces Round #236 (Div. 2)
    FZU 2140 Forever 0.5
    HDU 1171 Big Event in HDU(DP)
    HDU 1160 FatMouse's Speed(DP)
    ZOJ 3490 String Successor
    ZOJ 3609 Modular Inverse
    ZOJ 3603 Draw Something Cheat
    ZOJ 3705 Applications
  • 原文地址:https://www.cnblogs.com/soundcode/p/12841506.html
Copyright © 2011-2022 走看看