• 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";
  • 相关阅读:
    UART协议
    芯片时钟体系(take example as s3c2440)
    PCIe协议
    I2C协议
    SPI协议
    嵌入式相关术语列表
    TreeView控件数据绑定之:数据库数据递归绑定
    连接SQL Server 数据库的三种方式
    JS小功能之:五角星评论
    MVC学习之开发技巧总结(1)
  • 原文地址:https://www.cnblogs.com/soundcode/p/12841506.html
Copyright © 2020-2023  润新知