簡體   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