简体   繁体   English

在C#中,如何将Dictionary绑定到DataGridView?

[英]In C#, How to bind a Dictionary to a DataGridView?

I have a DataGridView ( dgv_Orders ) and a Dictionary( orders<uint, Order> ). 我有一个DataGridView( dgv_Orders )和一个Dictionary( orders<uint, Order> )。 Class Order has the following members: uint order_id, uint volume, double price I would like to be able to have dgv_orders display all of the values contained in the dictionary, row by row, listing out all of their properties. 类订单具有以下成员: uint order_id, uint volume, double price我希望能够让dgv_orders逐行显示字典中包含的所有值,列出所有属性。 I need the Dictionary structure, so that I can edit the values in O(1) time. 我需要Dictionary结构,以便我可以在O(1)时间内编辑值。

I have gotten this to work with a BindingList & I have also looked at the following solution.. 我已经使用BindingList工作了,我也看了下面的解决方案..

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

and gotten it to almost work, except that the DataGridView just has the columns, Key & Value, whereas I would like it to show order_id, volume, & price. 并使它几乎工作,除了DataGridView只有列,键和值,而我希望它显示order_id,音量和价格。 Are there any ways to do so? 有没有办法这样做?

Thanks in advance! 提前致谢!

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));
    }
}

I think your only option is to bind to the Values property of the dictionary. 我认为你唯一的选择是绑定到字典的Values属性。

Well, actually, that's not entirely true. 嗯,实际上,这并不完全正确。 You could create a new class that inherits from dictionary and then implement the IBindingList or IObservableCollection interfaces. 您可以创建一个继承自字典的新类,然后实现IBindingList或IObservableCollection接口。

All of our core collection class inherit from System.Collections.ObjectModel.KeyedCollection which has similar performance characteristics as dictionary ( provides both O(1) indexed retrieval and keyed retrieval that approaches O(1) ), but is also more easily serialized over WCF and Web Services than dictionaries. 我们所有的核心集合类都继承自System.Collections.ObjectModel.KeyedCollection ,它具有与字典类似的性能特征( provides both O(1) indexed retrieval and keyed retrieval that approaches O(1) ),但也更容易在WCF上序列化和Web服务比词典。

Update 更新

Here is an example of a collection for your order class: 以下是订单类的集合示例:

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

I was able to successfully test this with the following code and a new, default datagridview: 我能够使用以下代码和一个新的默认datagridview成功测试它:

        var cOrders = new OrderCollection();

        cOrders.Add(new Order(1));
        cOrders.Add(new Order(2));
        cOrders.Add(new Order(3));

        dataGridView1.DataSource = cOrders;

Use this as a starting example and change the variables to match your usecase. 使用此作为开始示例并更改变量以匹配您的用例。

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

As others have pointed out, it is possible to bind to a Dictionary<T> , but is it reasonable? 正如其他人所指出的那样,可以绑定到Dictionary<T> ,但这是否合理? The purpose of a dictionary is to lookup values form a given key, what the DataGridView will not do. 字典的目的是从给定键查找值,DataGridView不会做什么。 A List<T> that allows access to individual items through an index seems more appropriate. 允许通过索引访问单个项目的List<T>似乎更合适。 A List<T> can also be sorted in a meaningful way, where as the dictionary will yield a completely 'wild' order based on hash values. List<T>也可以以有意义的方式排序,其中字典将基于散列值产生完全“狂野”的顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM