绑定到字典的DataGridView

绑定到字典的DataGridView,第1张

绑定字典的DataGridView

有两个问题

Dictionary
; 第一个是(如您所发现的)它没有实现必要的
IList
/
IListSource
。第二个原因是,没有对项目的保证顺序(实际上,也没有索引器),这使得无法通过索引(而不是键)进行随机访问。

但是…可能有些烟和镜子是可行的。如下所示:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Windows.Forms;static class Program{    [STAThread]    static void Main()    {        Dictionary<string, decimal> prices = new Dictionary<string, decimal>();        prices.Add("foo", 123.45M);        prices.Add("bar", 678.90M);        Application.EnableVisualStyles();        Form form = new Form();        DataGridView dgv = new DataGridView();        dgv.Dock = DockStyle.Fill;        form.Controls.Add(dgv);        var bl = prices.ToBindingList();        dgv.DataSource = bl;        Button btn = new Button();        btn.Dock = DockStyle.Bottom;        btn.Click += delegate        { prices.Add(new Random().Next().ToString(), 0.1M); bl.Reset();        };        form.Controls.Add(btn);        Application.Run(form);    }    public static DictionaryBindingList<TKey, TValue>        ToBindingList<TKey, TValue>(this IDictionary<TKey, TValue> data)    {        return new DictionaryBindingList<TKey, TValue>(data);    }    public sealed class Pair<TKey, TValue>    {        private readonly TKey key;        private readonly IDictionary<TKey, TValue> data;        public Pair(TKey key, IDictionary<TKey, TValue> data)        { this.key = key; this.data = data;        }        public TKey Key { get { return key; } }        public TValue Value        { get {     TValue value;     data.TryGetValue(key, out value);     return value; } set { data[key] = value; }        }    }    public class DictionaryBindingList<TKey, TValue>        : BindingList<Pair<TKey, TValue>>    {        private readonly IDictionary<TKey, TValue> data;        public DictionaryBindingList(IDictionary<TKey, TValue> data)        { this.data = data; Reset();        }        public void Reset()        { bool oldRaise = RaiseListChangedEvents; RaiseListChangedEvents = false; try {     Clear();     foreach (TKey key in data.Keys)     {         Add(new Pair<TKey, TValue>(key, data));     } } finally {     RaiseListChangedEvents = oldRaise;     ResetBindings(); }        }    }}

请注意,自定义扩展方法的使用完全是可选的,并且可以在C#2.0等中通过仅使用

newDictionaryBindingList<string,decimal>(prices)
来删除。



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4976324.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-14
下一篇2022-11-14

发表评论

登录后才能评论

评论列表(0条)

    保存