简体   繁体   中英

Binding Dictionary<String, List<String>> to DataGridView in C#

How can I bind a Dictionary<String, List<String>> to the DataGridView in C#?

Something like:

var l = from row in listValues select new { Item = row.key, Elements = row.Value };
DatagridView.dataSource = l;

Erk, I wouldn't.. but you could if you wanted:

yourDataGridView.DataSource = dictionary.Select(x => new {
  PK = x.Key,
  Name = x.Value[0];
  Email = x.Value[1];
  Address = x.Value[2];
}).ToArray();

Assuming that the List<string> in the dictionary's value is a list of 3 strings, being the person's name, email and address. ie the dictionary was created like/looks like:

var d = new Dictionary<string, List<string>> {
  {"person1", new List<string>{ "anna", "anna@mail.com", "1 the street" }},
  {"person2", new List<string>{ "caius", "caius@gmail.com", "2 the street" }}
};

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