Solved

Binding a Dictionary to a DataGridView?

Posted on 2011-03-19
4
1,337 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
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 35

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now