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();