Geeks With Blogs

News My Blog has been MOVED to https://mfreidge.wordpress.com

Michael Freidgeim's OLD Blog My Blog has been MOVED to https://mfreidge.wordpress.com

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;
        }
 
Posted on Tuesday, December 19, 2006 2:21 PM ASP.NET | Back to top


Comments on this post: Function to bind Generic Dictionary with dropdown list.

# re: Function to bind Generic Dictionary with dropdown list.
Requesting Gravatar...
YES! This helped me...
Left by Ralph Krausse on Feb 28, 2007 7:22 AM

# re: Function to bind Generic Dictionary with dropdown list.
Requesting Gravatar...
Really it helped me..
Thankkkkkkkks a lot
carry on good stuff
Left by Rajesh on Oct 01, 2007 1:50 AM

# re: Function to bind Generic Dictionary with dropdown list.
Requesting 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
Left by Sandeep Aparajit on Oct 02, 2009 12:36 PM

# re: Function to bind Generic Dictionary with dropdown list.
Requesting Gravatar...
thank you..
it helped me..
Left by deepthi on Feb 16, 2011 4:13 AM

# re: Function to bind Generic Dictionary with dropdown list.
Requesting 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";

Left by oleg on Mar 31, 2011 8:27 AM

# re: Function to bind Generic Dictionary with dropdown list.
Requesting Gravatar...
sorry,
need converting like this

DataSource = d.Select(r => new KeyValuePair<string, string>(r.Key, r.Value.Name)).ToList();
Left by oleg on Mar 31, 2011 8:39 AM

Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net