Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Bind distinct list to Dropdown List from DataTable

0.00/5 (No votes)
15 Sep 2011 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here