简体   繁体   中英

DataGridView bound to a Nested Dictionary

I've tried seeing this: DataGridView bound to a 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:

+------+-----------------+-----------------+---------------+
| 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 )

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.

So when the user changes something, they have to reopen the DataGridView or refresh it manually in order to see the new changes. 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.

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). 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.

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. 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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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