我有一个字典列表,我想绑定到C#winform
中的网格视图public class Person {
Dictionary<string, object> attributes = new Dictionary<string, object>();
public Dictionary<string, object> Attributes {
get { return attributes; }
}
}
List<Person> persons = new List<Person>();
Person person1 = new Person();
person1.Attributes.Add("name", "Ross");
person1.Attributes.Add("address", "Street 1");
persons.Add(person1);
Person person2 = new Person();
person2.Attributes.Add("name", "Tom");
person2.Attributes.Add("address", "Street 2");
persons.Add(person2);
那么我该如何使用这个字典将它绑定到网格视图。它在下面吗?
GridView1.DataSource = persons.Select(x => x.Attributes).ToList();
我想要的是网格将字符串(键)显示为列
例如
名字|地址
罗斯|街1号
汤姆|街2
有可能吗?
答案 0 :(得分:0)
GridView1.DataSource =
persons.Select(p => new
{name = p.Attributes["name"], address = p.Attributes["address"]});
答案 1 :(得分:0)
试试这个
List<Person> persons = new List<Person>();
Person person1 = new Person();
person1.Attributes.Add("name", "Ross");
person1.Attributes.Add("address", "Street 1");
persons.Add(person1);
Person person2 = new Person();
person2.Attributes.Add("name", "Tom");
person2.Attributes.Add("address", "Street 2");
persons.Add(person2);
List<KeyValuePair<string, string>> items = persons.Select(person => new KeyValuePair<string, string>(person.Attributes["name"].ToString(), person.Attributes["address"].ToString())).ToList();
dataGridView1.DataSource = items;