在C#中将字典绑定到DataGridView? [英] Binding a Dictionary to the DataGridView in C#?

查看:87
本文介绍了在C#中将字典绑定到DataGridView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下的字典项

Dictionary<string, List<StrikePrice>>

其中

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

,我希望将此字典分配给 DataGridView

and I wish to assign this dictionary to the DataGridView

this.dataGridViewTest.DataSource = listSmiles;

我知道不能将字典分配给 DataSource ,因为它不是从 IList 接口派生的.

I understand that a dictionary can't be assigned to the the DataSource as this doesn't derive from the IList interface.

有什么办法可以将此字典元素分配给datagrid?

Is there any way I can assign this dictionary element to the 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是一个DataGridView.dTAV是字典.关键是日期(不重要),值是类.

gridTAV is a DataGridView. dTAV is a Dictionary. The key is a date (not important), and the value is a class.

Dictionary<DateTime, TotalAccountValue> dTAV = new Dictionary<DateTime, TotalAccountValue>();

由于该值是一个类,因此"ToArray()"方法对我不起作用,因为它没有解包"该类的属性.

Since the value was a class the "ToArray()" method did not work for me, since it didn't "unpack" the class properties.

请注意,这不会将KEY放置在网格中,但是我并不是真的需要它.

Note that this does not place the KEY in the grid, but I didn't really need that.

这篇关于在C#中将字典绑定到DataGridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆