Few random development Tips-N-Tricks-6

Usually, to bind a dropdownlist, when we need multiple values, as a part of it’s DataValueField, either we run a foreach loop on ‘Dropdownlist.Items.Add’ OR in the SQL level change the SELECT statement to concatenate multiple fields. This example below, uses DataTable->Expression, to bind multiple values to Dropdownlist.

private void BindMultipleColumns()
{
 DataTable dt = GetSelectOutputORspOutput();
 dt.Columns.Add("NewIdColumn", System.Type.GetType("System.String"));
 dt.Columns["NewIdColumn"].Expression = "ColName1+','+ColName2";
 dt.AcceptChanges();
 ddl1.DataSource = dt;
 ddl1.DataTextField = "ColName4";
 ddl1.DataValueField = "NewIdColumn";
 ddl1.DataBind();
}

Thanks for reading.

This entry was posted in Tips-n-Tricks. Bookmark the permalink.

Leave a comment