【问题标题】:In C#, How to bind a Dictionary to a DataGridView?在 C# 中,如何将字典绑定到 DataGridView?
【发布时间】:2012-02-09 03:12:19
【问题描述】:

我有一个 DataGridView (dgv_Orders) 和一个 Dictionary(orders<uint, Order>)。类 Order 具有以下成员:uint order_id, uint volume, double price 我希望能够让 dgv_orders 逐行显示字典中包含的所有值,并列出它们的所有属性。我需要 Dictionary 结构,以便我可以在 O(1) 时间内编辑值。

我已经让它与 BindingList 一起使用,并且我还查看了以下解决方案..

https://stackoverflow.com/a/854969/1143465

并让它几乎可以工作,除了 DataGridView 只有列键和值,而我希望它显示 order_id、数量和价格。有什么办法吗?

提前致谢!

class Order : INotifyPropertyChanged
{
    private uint order_id;
    private double price;
    private uint volume;

    public event PropertyChangedEventHandler PropertyChanged;

    public Order(uint order_id)
    {
        this.order_id = order_id;
        price = 10.0;
        volume = 1;
    }

    public uint Order_ID
    {
        get { return order_id; }
        set { order_id = value; NotifyPropertyChanged("Order_ID"); }
    }

    public uint Price
    {
        get { return price; }
        set { price = value; NotifyPropertyChanged("Price"); }
    }

    public uint Volume
    {
        get { return volume; }
        set { volume = value; NotifyPropertyChanged("Volume"); }
    }

    private void NotifyPropertyChanged(string name)
    {
         if(PropertyChanged != null)
             PropertyChanged(this, new PropertyChangedEventArgs(name));
    }
}

【问题讨论】:

    标签: c# dictionary datagridview


    【解决方案1】:

    我认为您唯一的选择是绑定到字典的Values 属性。

    嗯,实际上,这并不完全正确。您可以创建一个继承自字典的新类,然后实现 IBindingList 或 IObservableCollection 接口。

    我们所有的核心集合类都继承自 System.Collections.ObjectModel.KeyedCollection,它具有与字典 (provides both O(1) indexed retrieval and keyed retrieval that approaches O(1)) 相似的性能特征,但也比字典更容易通过 WCF 和 Web 服务进行序列化。

    更新

    以下是您的订单类的集合示例:

    public class OrderCollection : System.Collections.ObjectModel.KeyedCollection<uint, Order>
    {
        protected override uint GetKeyForItem(Order item)
        {
            return item.Order_ID;
        }
    }
    

    我能够使用以下代码和一个新的默认数据网格视图成功测试:

            var cOrders = new OrderCollection();
    
            cOrders.Add(new Order(1));
            cOrders.Add(new Order(2));
            cOrders.Add(new Order(3));
    
            dataGridView1.DataSource = cOrders;
    

    【讨论】:

    • 您可以绑定到字典.. 但是.. 您使用的数据源是什么..?你可以发布一些代码..?
    • @DJKRAZE 数据源是字典,我将通过我的代码对其进行修改。你问的是这个吗?
    • @DJKRAZE:是的,你可以绑定到字典,但是 IENumerator 的实现只暴露了底层的 KeyValuePair,所以除非你有一个简单的值(即字符串),否则它不会给你大量的好处。
    • competent_tech 是正确的。我也不确定 OP 将其绑定到字典后到底在寻找什么。也许他应该将他的数据绑定到某种数据适配器之类的东西上。 . 你有什么想法。
    • @Joshua:对不起,应该更清楚。除了序列化,这个集合还实现了 IList,它返回底层记录(即 Orders 列表),而不是字典所做的 KeyValuePair 集合(即 KeyValuePair 列表)。
    【解决方案2】:

    将此作为起始示例并更改变量以匹配您的用例。

    Dictionary<string, double> someDictionary = new Dictionary<string, double>();
    BindingSource _bindingSource = new BindingSource();
    dataGridView1.DataSource = _bindingSource;
    _bindingSource.DataSource = someDictionary;
    

    【讨论】:

      【解决方案3】:

      正如其他人指出的那样,可以绑定到Dictionary&lt;T&gt;,但这合理吗?字典的目的是从给定的键中查找值,而 DataGridView 不会这样做。允许通过索引访问单个项目的List&lt;T&gt; 似乎更合适。 List&lt;T&gt; 也可以以有意义的方式排序,因为字典将根据哈希值产生完全“狂野”的顺序。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-01
        相关资源
        最近更新 更多