Function to bind Generic Dictionary with dropdown list.

With the tip from the post How to bind Generic Dictionary with dropdown list  I've created the static method:

          //'http://blogs.msdn.com/piyush/archive/2006/10/17/how-to-bind-generic-dictionary-with-dropdown-list.aspx

        public static void ControlBinding(ListControl ctrl, Dictionary<string, string> dict)

        {

            ctrl.DataSource = dict;

            ctrl.DataValueField = "Key";

            ctrl.DataTextField= "Value";

        }

//Overload for StringDictionary      
 public static void ControlBinding(ListControl ctrl, StringDictionary dict)
        {
            ctrl.DataSource = dict;
            ctrl.DataValueField = "Key";
            ctrl.DataTextField = "Value";
        }
 

 Hopefully, it would help someone.

 Similar method for table:

       public static void ControlBinding(ListControl ctrl, DataTable tbl,string valueField, string textField)
        {
            ctrl.DataSource = tbl;
            ctrl.DataValueField = valueField;
            ctrl.DataTextField = textField;
        }
 
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted @ Tuesday, December 19, 2006 2:21 PM
Print

Comments on this entry:

# re: Function to bind Generic Dictionary with dropdown list.

Left by Ralph Krausse at 2/28/2007 7:22 AM
Gravatar
YES! This helped me...

# re: Function to bind Generic Dictionary with dropdown list.

Left by Rajesh at 10/1/2007 1:50 AM
Gravatar
Really it helped me..
Thankkkkkkkks a lot
carry on good stuff

# re: Function to bind Generic Dictionary with dropdown list.

Left by Sandeep Aparajit at 10/2/2009 12:36 PM
Gravatar
Great stuff Michael!
I would suggest you to expand this article to all possible data sources (including the derived class examples). That would become a great soruce for developers. Anyways Thanks for the info!

Sandeep Aparajit
http://sandeep-aparajit.blogspot.com

# re: Function to bind Generic Dictionary with dropdown list.

Left by deepthi at 2/16/2011 4:13 AM
Gravatar
thank you..
it helped me..

# re: Function to bind Generic Dictionary with dropdown list.

Left by oleg at 3/31/2011 8:27 AM
Gravatar
if we have not simple Dictionary
For example Dictionary<int, Car>

class Car {int Id, string Name, string Color}

ctrl.DataSource = dict;

ctrl.DataValueField = "Key";

ctrl.DataTextField= ??? /// "Value";

# re: Function to bind Generic Dictionary with dropdown list.

Left by oleg at 3/31/2011 8:39 AM
Gravatar
sorry,
need converting like this

DataSource = d.Select(r => new KeyValuePair<string, string>(r.Key, r.Value.Name)).ToList();

Your comment:



(not displayed)


 
 
 
 
 

Live Comment Preview:

 
«February»
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910