?
Solved

Binding a Dictionary to a DataGridView?

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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

719 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