Wednesday, June 13, 2012

Creating a Multi-Text DropDownList from general ASP.NET DropDownList

In some projects there may be a requirement to bind multiple data fields with some pattern as the datatextfield such as "Name - Address". To achieve this requirement new column can be added to the datatable before bind data to the dropdownlist. Following C# code can be use to achieve this requirement.

DataColumn newColumn = new DataColumn("AddedColumn"); // Create a new data column
newColumn.Expression = "Name + ' - ' + Address"; //Name and Address are columns in datatable
dtData.Columns.Add(newColumn); //dtData is the datatable

ddDropDownList.DataTextField = "AddedColumn";

ddDropDownList.DataSource = dtData;
ddDropDownList.DataBind();

Like this any formatted string can be created using data table columns and bind to the DropDownList as the DataTextField. 


No comments:

Post a Comment