[英]Binding a Dictionary to the DataGridView in C#?
I have a dictionary item as below 我有一个字典项目如下
Dictionary<string, List<StrikePrice>>
where 哪里
public class StrikePrice
{
public string Strike { get; private set; }
public string Price { get; private set; }
public StrikePrice(string strike, string price)
{
Strike = strike;
Price = price;
}
}
and I wish to assign this dictionary to the DataGridView
我希望将此字典分配给
DataGridView
this.dataGridViewTest.DataSource = listSmiles;
I understand that a dictionary can't be assigned to the the DataSource
as this doesn't derive from the IList
interface. 我知道无法将字典分配给
DataSource
因为它不是从IList
接口派生的。
Is there any way I can assign this dictionary element to the datagrid? 有什么办法可以将这个字典元素分配给datagrid吗?
I know this is a bit old, but perhaps it will help someone. 我知道这有点旧,但也许它会对某人有所帮助。 This one line solution worked for me
这一行解决方案对我有用
gridTAV.DataSource = dTAV.Values.ToList<TotalAccountValue>();
gridTAV is a DataGridView. gridTAV是一个DataGridView。 dTAV is a Dictionary.
dTAV是一个字典。 The key is a date (not important), and the value is a class.
关键是日期(不重要),值是一个类。
Dictionary<DateTime, TotalAccountValue> dTAV = new Dictionary<DateTime, TotalAccountValue>();
Since the value was a class the "ToArray()" method did not work for me, since it didn't "unpack" the class properties. 由于该值是一个类,“ToArray()”方法对我来说不起作用,因为它没有“解包”类属性。
Note that this does not place the KEY in the grid, but I didn't really need that. 请注意,这不会将KEY放在网格中,但我并不真的需要它。
您是否尝试过使用Dictionary的Values属性?
this.dataGridViewTest.DataSource = listSmiles.Values.ToList();
Yes ,by Calling ToArray of Dictionary 是的,通过Calling ToArray of Dictionary
var g = this.dataGridView1;
var s = new Dictionary<string, string>();
s.Add("1", "Test1");
s.Add("2", "Test2");
s.Add("3", "Test3");
g.DataSource = s.ToArray();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.