Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
See more:
How to bind dictionary items into datagridview in windows forms using C#.


For example

Consider



C#
Dictionary<string, double,double,double> d =new Dictionary<string,int>()
	{
	    {"Apple",255,0,0},
	    {"Orange",255,165,0},
	    {"Grapes", 238,130,238}
	    
	};


I want to add them in a datagridview.

How to do this
Posted

First, that code won't compile because Dictionary will only take a single Key type and a single Value type, not a Key Type and 3 Value types.

Next, you can't bind a DGV to a Dictionary. A dictionary is 2 collections, one of keys and another of values. It's not a single collection of Key-Value.

You would have to convert the items in the dictionary to some other data structure, like a DataTable or List<t,> or any other type that implements one of the following interfaces: IList, IListSource, IBindingList or IBindingListView. The Dictionary<k,v> class does not do that.
 
Share this answer
 
Well you can use Array or List as a DataSource to your Grdiview. So get all the elements as a list and bind it to the grid.
C#
dataGridView.DataSource =  (from d in dictionary orderby d.Value select new { d.Key, d.Value}).ToList();


-KR
 
Share this answer
 
Comments
dcarl661 12-Apr-18 19:53pm    
solution 1 is incomplete, i get this error on the second dictionary entry
At least one object must implement IComparable.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900