Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Binding a Dictionary to a DataGridView?

Posted on 2011-03-19
4
Medium Priority
?
1,374 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 36

Accepted Solution

by:
Miguel Oz earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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.
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Screencast - Getting to Know the Pipeline

971 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