简体   繁体   English

C#-字典 <string, List<string> &gt;到DataGrid

[英]c# - Dictionary<string, List<string>> to DataGrid

i have the following problem: 我有以下问题:

I have a populated dictionary of lists, each lift has a specified, known length, and contains only strings, an example element would be: 我有一个列表填充字典,每个电梯都有一个指定的已知长度,并且仅包含字符串,示例元素为:

d1[key] = [ "Text1", "Text2", "Text3", "Text4", "0", "0", "0", "0", "0" ]

The datagrid will have predeclared columns that correspond to the key, and each of the 8 list elements, for a total of 9 columns. 数据网格将具有与键对应的预声明列,以及8个列表元素中的每一个,总共9列。

I have written this to try and populate the DataGrid, is there a more efficient way, basicly writing every single line onto the datagrid. 我已经写过这篇文章来尝试填充DataGrid,有没有更有效的方法,基本上是将每一行写到datagrid上。 The Dictionary may have upwards of 1k keys though. 字典可能有超过1k个键。

public static void DictionaryToDataGrid(Dictionary<string, List<string>> inputdict1)
    {
        Dictionary<string, List<string>> d1 = inputdict1;

        foreach (KeyValuePair<string, List<string>> item in d1)
        {
            DatagridForm.grid.Rows.Add(item.Key, item.Value[0], item.Value[1], item.Value[2]);
        }
    }

Is there a quicker, more efficient way to do this? 有更快,更有效的方法来做到这一点吗? Thank you. 谢谢。

A shorter version would be: 较短的版本是:

    foreach (KeyValuePair<string, List<string>> item in inputdict1)
        DatagridForm.grid.Rows.Add(item.Key, item.Value[0], item.Value[1], item.Value[2]);

It is also a bit more efficient since you're not creating a new variable d1 , and referencing the contents of inputdict1 to it (thanks apocalypse). 由于您没有创建新变量d1而是inputdict1的内容引用到该变量(感谢启示录),因此效率更高。

Hope this helps. 希望这可以帮助。

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

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