Hi,
To display the two data from the datatable columns in your dropdownlist, I suggest that you format the data before bind it. Use linq for this.
This is the easiest way.
Here is an example of this suggestion.
HTML
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddl_room" runat="server">
</asp:DropDownList>
</div>
</form>
CODE BEHIND
private DataTable SelectData()
{
DataTable dt = new DataTable();
dt.Columns.Add("Room_Code", typeof(string));
dt.Columns.Add("Room_Capacity", typeof(string));
DataRow dr = dt.NewRow();
dr["Room_Code"] = "20";
dr["Room_Capacity"] = "4";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Room_Code"] = "21";
dr["Room_Capacity"] = "2";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Room_Code"] = "22";
dr["Room_Capacity"] = "1";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Room_Code"] = "1";
dr["Room_Capacity"] = "10";
dt.Rows.Add(dr);
return dt;
}
private void CreateDataSourceFillDropList(DataTable dt)
{
var datasource = from x in dt.AsEnumerable()
select new {
Code = x.Field<string>("Room_Code")
,DisplayField = String.Format("Room: {0} Capacity: {1}", x.Field<string>("Room_Code").PadLeft(2, '0'), x.Field<string>("Room_Capacity").PadLeft(2, '0'))
};
this.ddl_room.DataTextField = "DisplayField";
this.ddl_room.DataValueField = "Code";
this.ddl_room.DataSource = datasource;
this.ddl_room.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.CreateDataSourceFillDropList(this.SelectData());
}
}