Click here to Skip to main content
15,507,266 members
Articles / Programming Languages / C#
Tip/Trick
Posted 14 Sep 2011

Tagged as

Stats

40.7K views
2 bookmarked

Bind distinct list to Dropdown List from DataTable

Rate me:
Please Sign up or sign in to vote.
4.20/5 (4 votes)
15 Sep 2011CPOL
Bind distinct list to Dropdown List from DataTable
Sometimes, we need to bind distinct list from datatable retrieved from dropdown list from Datatable retrieved data from database:
C#
DataView view = new DataView(table);
DataTable distinctValues = view.ToTable(true, "Column1", "Column2" ...);

OR
C#
table.DefaultView.ToTable(true, "Column1", "Column2" ...);

The sample code for this is as below:
C#
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)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionbind distinct values Pin
Manohar_manu3-Jun-14 0:38
Manohar_manu3-Jun-14 0:38 
QuestionIts worked for me Pin
Manohar_manu3-Jun-14 0:37
Manohar_manu3-Jun-14 0:37 
Its worked for me
Manohara

GeneralMy vote of 5 Pin
IronRed25-Sep-12 6:55
IronRed25-Sep-12 6:55 
GeneralMy vote of 1 Pin
d347hm4n5-Aug-12 23:59
d347hm4n5-Aug-12 23:59 

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

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