如何哈希表绑定到一个下拉列表? [英] How can a hashtable be bound to a drop down list?

查看:152
本文介绍了如何哈希表绑定到一个下拉列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在vb.net /的WinForms,怎能一个Hashtable绑定到一个下拉列表或任何其他数据源驱动的控制?

In vb.net / winforms, how can a hashtable be bound to a drop down list or any other datasource-driven control?

推荐答案

这是的WinForms,WPF,或asp.net? [更新:啊...的WinForms ;-p]

Is this winforms, wpf, or asp.net? [update: ahh... winforms ;-p]

的WinForms想要的数据是的IList (或间接地通过 IListSource ) - 所以我猜(从评论)所使用的WinForms。没有一个内置字典的集合实施的IList ,但说实话这并不重要:如果你是数据绑定,体积大概是相当小的,所以常规列表应该就可以了。

winforms wants data to be IList (or, indirectly, via IListSource) - so I'm guessing (from the comment) that you are using winforms. None of the inbuilt dictionary-like collections implement IList, but to be honest it doesn't matter: if you are data-binding, the volume is probably fairly small, so a regular list should be fine.

最好的办法是像一个名单,其中,T> 的BindingList< T> ,其中 T 拥有所有你要绑定的属性。这是一个选择吗?如果你被卡住1.1(因为你提到的HashTable ,而不是词典<,> ),然后使用的ArrayList

The best option is something like a List<T> or BindingList<T>, where T has all the properties you want to bind to. Is this an option? If you are stuck with 1.1 (since you mention HashTable rather than Dictionary<,>), then use ArrayList.

例(在C#):​​

class MyData
{
    public int Key { get; set; }
    public string Text { get; set; }
}
[STAThread]
static void Main()
{
    var data = new List<MyData>
    {
        new MyData { Key = 1, Text = "abc"},
        new MyData { Key = 2, Text = "def"},
        new MyData { Key = 3, Text = "ghi"},
    };
    ComboBox cbo = new ComboBox
    {
            DataSource = data,
            DisplayMember = "Text",
            ValueMember = "Key"
    };
    cbo.SelectedValueChanged += delegate {
        Debug.WriteLine(cbo.SelectedValue);
    };
    Application.Run(new Form {Controls = {cbo}});
}

这篇关于如何哈希表绑定到一个下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆