繁体   English   中英

绑定到嵌套字典的DataGridView

[英]DataGridView bound to a Nested Dictionary

我试着看到这个: DataGridView绑定到Dictionary

但这并不是我要找的东西,所以我在这里。

我有一个这样的字典(只是一个看起来像的例子):

{
    "file": 
    {
        "var-type": 
            {
                "var1": 
                       {
                           "description": "the description here",
                           "length": "the length here",
                           "type": "the type here",
                           "line": "the line number here",
                       }
            }
    }
}

并希望有一个如下所示的DataGridView (实时,因此绑定它):

+------+-----------------+-----------------+---------------+
| Name |   Description   |     Length      |     Type      |
+------+-----------------+-----------------+---------------+
| var1 | the description | the length here | the type here |
+------+-----------------+-----------------+---------------+

DataGridView将位于TabPage (在TabController内部,并且该标签页的名称为var-type

最初我有一个字典,每当用户想要它的一个DataGridView时, DataGridView就会一次次从零开始。

因此,当用户更改某些内容时,他们必须重新打开DataGridView或手动刷新它才能看到新的更改。 我不要这个 所以,我想有DataTables为每个var-type (并使用Dictionary作为一个DataTable集合,每当他们做一些修改,用户将获得实时变化。

效果很好。 但是,当涉及到编辑特定变量的信息时,我将不得不使用.Select() (而且我无法将行号存储在同一DataTable中)。 我想避免这种情况,所以我又回到了所有的一本字典。 我还听说字典更轻巧。

在使用DataTables以避免.Select() ,我也尝试存储每个变量的行号,但是这种方法也有一些问题,我确实更愿意使用字典。

我不想让DataGridView每次用户想要看到它时都使用for循环(因此,我将DataTables用作可以在任何地方访问的属性,并在需要显示它们时将它们用作DataSource ),我只希望能够随时使用它们。 这可能吗?

仅作记录; 是的,这是一个解析器。 非常感谢!

使用以下之一:

            DataTable dt = new DataTable();

            Dictionary<string, DataRow> dict1 = dt.AsEnumerable()
                .GroupBy(x => x.Field<string>("Name"), y => y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

            Dictionary<string, List<DataRow>> dict2 = dt.AsEnumerable()
                .GroupBy(x => x.Field<string>("Name"), y => y)
                .ToDictionary(x => x.Key, y => y.ToList());

暂无
暂无

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

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