在下拉列表中显示两列 [英] Display two column in a dropdown list

查看:664
本文介绍了在下拉列表中显示两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨所有

i有一个下拉列表,它给出一个带有数据源的表的2列

表:房间

列:Room_Code, Room_Capacity

i需要显示类似这样的东西

房间:20容纳人数:4

房间:21容纳人数:2






下拉列表中的


我该怎么办?



我尝试过的事情:



i在这个网站搜索并找到一些但不工作在所有

i阅读此 ASP的多列DropDown。 NET [ ^ ]

但它的vb.net我需要C#代码

hi all
i have a dropdown list that give 2 column of a table with a datasource
table: Room
columns : Room_Code,Room_Capacity
i need to display something like this
Room : 20 Capacity : 4
Room : 21 Capacity : 2
.
.
.
in the drop down list
what can i do?

What I have tried:

i search in this site and find someways but not working at all
i read this Multiple Columns DropDown for ASP.NET[^]
but its vb.net i need C# code

推荐答案







显示dropdownlis中数据表列的两个数据t,我建议您在绑定之前格式化数据。使用linq。

这是最简单的方法。



这是一个这个建议的例子。



HTML

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


这篇关于在下拉列表中显示两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆