How to bind a Dictionary to DataSource of DataGridView

c#, datagridview, dictionary

Solution

Check out the docs for the DataSource property. It only handles specific types (IList, IListSource etc.). So you cannot bind it to an IDictionary. So, this will work:

List<KeyValuePair<string, string>> d = new List<KeyValuePair<string, string>>();
d.Add(new KeyValuePair<string, string>("1", "2323"));
d.Add(new KeyValuePair<string, string>("2", "1112323"));

DataGridView v = new DataGridView();
v.DataSource = d;

Problem

I think that question is clear. I have a Dictionary instance and I want to bind it like DataSource of a DataGridView instance. Actually I can bind it straight this way: ``` Dictionary<string,string> d = new Dictionary<string,string>(); d.Add("1","test1"); d.Add("2","test2"); DataGridView v = new DataGridView(); v.DataSource = d; ``` But without any results.

Original source