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.

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s