如何将下拉列表与表绑定 [英] How to bind dropdownlist with the tables

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

问题描述

实际上我在数据库中有五个表,如何将这些表绑定到下拉列表,请问有人可以告诉我吗?

Actually i have five tables in the database how can be i bind that tables to dropdownlist please can any one tell me ?

推荐答案



如果可以的话请尝试...

Hi,

Try this if could help...

protected void BindProvince()
{
     List<province> lst = new List<province>();
     lst = cmd.GetLookupProvince();
     Province addl = new Province();
     addl.PROVINCE_ID = 0;
     addl.PROVINCE_NAME = "--Select One--";
     lst.Insert(0, addl);
     ddlProvince.DataValueField = "PROVINCE_ID"; 
     ddlProvince.DataTextField = "PROVINCE_NAME";
     ddlProvince.DataSource = lst;
     ddlProvince.DataBind();
}



如果可以的话请投票...

问候,



Please vote if could help...

Regards,


首先,您需要了解 ^ ].它包含两个重要属性:
1. DataValueField
2. DataTextField

对于DataValueField DataTextField 属性,如果仅将值设置为其中一个,则另一个属性也将设置为相同的值.如果仅指定DropDownList1.DataTextField = "CategoryName",则每个DropDownList项目的显示文本和值都将设置为Name.如果仅指定DropDownList1. DataValueField = "CategoryID",则每个DropDownList项目的显示文本和值都将设置为ID.
看到这个:
First you need to understand the flow of DropDownList[^]. It contains two important properties:
1. DataValueField
2. DataTextField

For the DataValueField and DataTextField properties, if you only set value to one of them, the other property is also set to the same value. If you only specify DropDownList1.DataTextField = "CategoryName", both the display text and value of each DropDownList item are set to Name. If you only specify DropDownList1. DataValueField = "CategoryID", both the display text and value of each DropDownList item are set to ID.
See this:
<asp:dropdownlist id="DropDownList1" runat="server" xmlns:asp="#unknown">
</asp:dropdownlist>


protected void Page_Load(object sender, EventArgs e)
{
    using (SqlConnection conn = new SqlConnection(@"Data Source=.;Initial Catalog=testing;Integrated Security=True;"))
    {
        SqlCommand cmd = new SqlCommand("select CategoryID,CategoryName from Categories",conn);

        DataTable dt = new DataTable();

        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        sda.Fill(dt);
        DropDownList1.DataSource = dt;
        DropDownList1.DataTextField = "CategoryName";
        DropDownList1.DataValueField = "CategoryID";
        DropDownList1.DataBind();
    }
}





--Amit





--Amit


您可以对所有表使用以下方法:(如果要将所有五个表的数据放在一个下拉列表中)
1.将所有表添加到数据集中.
2.使用以下代码:
You can use following method for all of your tables:(If you want data of all five tables in one drop down)
1. Add all your tables to your DataSet.
2. use following code:
foreach(Table yourTable in DataSet.Tables)
{
    foreach(DataRow row in yourTable.Rows)
    {
          ListItem item = new ListItem();
          item.Text=row["your column"].ToString();
          item.Value=row["your column"].ToString();
          yourDropDownList.Items.Add(item);
    }
}



如果要在不同的DropDownList中使用不同的表,则使用上面提到的解决方案2 .



If you want different tables in different DropDownList then use Solution 2 mentioned above.


这篇关于如何将下拉列表与表绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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