Click here to Skip to main content
Click here to Skip to main content

Bind distinct list to Dropdown List from DataTable

By , 15 Sep 2011
Rate this:
Please Sign up or sign in to vote.
Sometimes, we need to bind distinct list from datatable retrieved from dropdown list from Datatable retrieved data from database:
DataView view = new DataView(table);
DataTable distinctValues = view.ToTable(true, "Column1", "Column2" ...);
OR
table.DefaultView.ToTable(true, "Column1", "Column2" ...);
The sample code for this is as below:
DataTable dt = GetColors();
 
ddlColor.DataTextField = "ColorName";
ddlColor.DataValueField = "ColorID";
ddlColor.DataSource = dt.DefaultView.ToTable(true, "ColorID",  
                                                   "ColorName"); 
ddlColor.DataBind();

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

AditSheth
Software Developer
India India
No Biography provided
Follow on   Twitter   Google+

Comments and Discussions

 
GeneralMy vote of 5 PinmemberIronRed25-Sep-12 5:55 
It worked for me. Using the 3rd box as an example, I was able to pop my ddl from dt.
GeneralMy vote of 1 Pinmemberd347hm4n5-Aug-12 22:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 15 Sep 2011
Article Copyright 2011 by AditSheth
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid