简体   繁体   中英

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.

I have written this to try and populate the DataGrid, is there a more efficient way, basicly writing every single line onto the datagrid. The Dictionary may have upwards of 1k keys though.

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

Hope this helps.

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