Wil*_*llH 30 .net c# dictionary datagridview winforms
我有一个Dictionary包含物品和价格.这些项目是唯一的,但在应用程序的生命周期中慢慢添加和更新(也就是说,我事先不知道项目字符串).我想将此结构绑定到DataGridView,因此我可以在我的表单上显示更新,如:
Dictionary<string, double> _priceData = new Dictionary<string, double>();
BindingSource _bindingSource = new BindingSource();
dataGridView1.DataSource = _bindingSource;
_bindingSource.DataSource = _priceData;
Run Code Online (Sandbox Code Playgroud)
但不能,因为Dictionary没有实现IList(或IListSource,IBindingList或IBindingListView).
有没有办法实现这个目标?我需要保留一个唯一的项目列表,但也要更新现有项目的价格,因此Dictionary我认为a 是理想的数据结构,但我找不到在我的表单上显示数据的方法.
更新:
Marc的建议下面的工作非常好,但我仍然不确定如何在执行期间更新DataGridView.
我有一个类级变量:
private DictionaryBindingList<string, decimal> bList;
Run Code Online (Sandbox Code Playgroud)
然后在Main():中实例化
bList = new DictionaryBindingList<string,decimal>(prices);
dgv.DataSource = bList;
Run Code Online (Sandbox Code Playgroud)
然后在程序执行期间,如果将新条目添加到字典中:
prices.Add("foobar", 234.56M); bList.ResetBindings();
Run Code Online (Sandbox Code Playgroud)
我认为这将刷新DataGridView.为什么不?
Chr*_*ris 63
或者,在LINQ中,它很好很快:
var _priceDataArray = from row in _priceData select new { Item = row.Key, Price = row.Value };
Run Code Online (Sandbox Code Playgroud)
然后应该可绑定到列'Item'和'Price'.
要在网格视图中将其用作数据源,您只需使用它即可ToArray().
dataGridView1.DataSource = _priceDataArray.ToArray();
Run Code Online (Sandbox Code Playgroud)
Mar*_*ell 23
有几个问题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();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,使用自定义扩展方法完全是可选的,只需使用C#2.0等即可删除new DictionaryBindingList<string,decimal>(prices).
我想这会解决我几个月前遇到的问题。
使用字典,因为您想要更新商品价格,并且当您完成更新并想要在数据网格中显示时,只需执行此操作。希望能帮助你
Grd.DataSource=null;
Grd.DataSource = Dictionary.Values.ToList();
Run Code Online (Sandbox Code Playgroud)
可能这是最简单的方法:
Dictionary<char, double> myList = new Dictionary<char, double>();
dataGridView1.Columns.Add("Key", "KEY");
dataGridView1.Columns.Add("Values", "VALUES");
foreach (KeyValuePair<char,double> item in , myList)
{
dataGridView1.Rows.Add(item.Key, item.Value);
}
Run Code Online (Sandbox Code Playgroud)
如果使用这个,你的datagridview应该是可排序的.
| 归档时间: |
|
| 查看次数: |
69983 次 |
| 最近记录: |