简体   繁体   English

绑定到嵌套字典的DataGridView

[英]DataGridView bound to a Nested Dictionary

I've tried seeing this: DataGridView bound to a Dictionary 我试着看到这个: DataGridView绑定到Dictionary

But it's not exactly what I'm looking for, so here I am. 但这并不是我要找的东西,所以我在这里。

I have a dictionary like this (just an example of how it looks like): 我有一个这样的字典(只是一个看起来像的例子):

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

And would like to have a DataGridView (real-time, thus binding it) that looks like this: 并希望有一个如下所示的DataGridView (实时,因此绑定它):

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

This DataGridView will be in a TabPage (inside a TabController , and that tab page's name is var-type ) DataGridView将位于TabPage (在TabController内部,并且该标签页的名称为var-type

At first I had a dictionary, and every time the user wanted a DataGridView of it, the DataGridView would be made again and again from zero. 最初我有一个字典,每当用户想要它的一个DataGridView时, DataGridView就会一次次从零开始。

So when the user changes something, they have to reopen the DataGridView or refresh it manually in order to see the new changes. 因此,当用户更改某些内容时,他们必须重新打开DataGridView或手动刷新它才能看到新的更改。 I don't want this. 我不要这个 So I tried having DataTables for each var-type (and using a Dictionary as a DataTable collection, and the user would get real-time changes whenever they edit something. 所以,我想有DataTables为每个var-type (并使用Dictionary作为一个DataTable集合,每当他们做一些修改,用户将获得实时变化。

That worked pretty well. 效果很好。 But when it comes to editing a specific var's information, I would have to use .Select() (and I can't store the line number in the same DataTable). 但是,当涉及到编辑特定变量的信息时,我将不得不使用.Select() (而且我无法将行号存储在同一DataTable中)。 I would like to avoid this, so I went back to a one-dictionary-for-all again. 我想避免这种情况,所以我又回到了所有的一本字典。 I also heard that a dictionary is much more lightweight. 我还听说字典更轻巧。

I've also tried storing the line number for each variable when using DataTables to avoid .Select() , but I also have some problems with this method, and would really prefer to have a dictionary instead. 在使用DataTables以避免.Select() ,我也尝试存储每个变量的行号,但是这种方法也有一些问题,我确实更愿意使用字典。

I don't want to make the DataGridView and use a for loop each time the user wants to see it (hence I went with DataTables as properties that I can access anywhere, and just use them as DataSource whenever I want to display them), I just want to be able to use them whenever I want. 我不想让DataGridView每次用户想要看到它时都使用for循环(因此,我将DataTables用作可以在任何地方访问的属性,并在需要显示它们时将它们用作DataSource ),我只希望能够随时使用它们。 Is this possible? 这可能吗?

Just for the record; 仅作记录; yeah this is a parser. 是的,这是一个解析器。 And thanks a lot! 非常感谢!

Use one of the following : 使用以下之一:

            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