Solved

Binding a Dictionary to a DataGridView?

Posted on 2011-03-19
4
1,352 Views
Last Modified: 2012-06-27
I'm trying to make a simple test application that will display the contents of a Dictionary in a DataGridView. Not sure if it's possible or what I need to add.

Say I have a simple class to store some data:
    class Myclass
    {
        private string fn;
        private string comment;
        public string Fn
        {
          get { return fn; }
          set { fn = value; }
        }
        public string Comment
        {
          get { return comment; }
          set { comment = value; }
        }
    }

Open in new window

(I think there's a shorter method of declaring these get/set pairs but I forget how it's done. Do you know how to shorten this?)

And then I have a dictionary containing some members of my class
            Dictionary<string, Myclass> mydic = new Dictionary<string, Myclass>();
            var d1 = new Myclass();
            d1.Fn = "Hello World";
            d1.Comment = "A Comment #1";
            mydic.Add("key1", d1);

Open in new window

Now I think I want to bind this dictionary to my DataGridView so I can see what's in the dictionary, but I'm not sure how to do this in Visual Studio (or manually for that matter).
0
Comment
Question by:deleyd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 7

Expert Comment

by:dimaj
ID: 35172870
Why use a dictionary? Why don't you just use a List<Myclass>. Then, do a data binding that would bind your list to your data grid.

If not, you can always resort to using Linq.
0
 
LVL 5

Expert Comment

by:morgulo
ID: 35172942
I think, You must create list grid.DataSource = new List<Myclass>(mydic.Values) and bind datasource every time dictionary changes or every time you want to see dictionary.
0
 

Author Comment

by:deleyd
ID: 35173281
OK so no direct binding to Dictionary? A dictionary (hash table) would be the ideal for storing this data, since it would allow quick random access to records by key. But to display the records,? Any samples of code showing how to display all the data records?
0
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 35174298
The reason you can't bind a dictionary to a DataGridView is that  the DataGridView.DataSource needs an object that implements IList, IListSource, thus generic list is needed
For a sample, check:
http://www.switchonthecode.com/tutorials/csharp-tutorial-binding-a-datagridview-to-a-collection.
An alternative using a derived dictionary is here:
http://stackoverflow.com/questions/854953/datagridview-bound-to-a-dictionary
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question