当前位置:编程学习 > C#/ASP.NET >>

关于向DropDownList 中添加内容选项,请大虾指点;

问题:怎么向DropDownList1中添加SQL中的字段内容;

例如下:
1、数据库名:WSFASP;数据表名:WSFABB;
2、表字段:ABB001,ABB002
3、数据库连接如下:
   SqlConnection conn = new SqlConnection("server=(local);user id=sa ;pwd=sa;database=WSFASP");
        conn.Open();
        SqlDataAdapter sqlsel = new SqlDataAdapter("select * from WSFABB",conn);
        DataSet ABD = new DataSet();
        sqlsel.Fill(ABD );

4、问题:DropDownList1.items.add()语句怎么写才能将表中字段ABB002的内容添加到DropDownList1中.

--------------------编程问答--------------------

DataSet ABD;

if(ADB != null && ADB.Tables > 0) {
    foreach(DataRow row in ADB.Tabls[0].Rows) {
        ListItem li = new ListItem();
        li.Text = row["ABB001"].ToString();
        li.Value = row["ABB002"].ToString();
        DropDownList1.Items.Add(li);
    }
}
--------------------编程问答-------------------- 设置数据源不是更简单
DropDownList1.datasource=Tabls --------------------编程问答--------------------

SqlConnection conn = new SqlConnection("server=(local);user id=sa ;pwd=sa;database=WSFASP");
  conn.Open();
  SqlDataAdapter sqlsel = new SqlDataAdapter("select * from WSFABB",conn);
  DataSet ABD = new DataSet();
  sqlsel.Fill(ABD );

this.DropDownList1.DataSource = ABD;
this.DropDownList1.DataTextField = "ABB002";
this.DropDownList1.DataValueField = "ABB001";
this.DropDownList1.DataBind();

--------------------编程问答-------------------- SqlConnection conn = new SqlConnection("server=(local);user id=sa ;pwd=sa;database=WSFASP");
  conn.Open();
  SqlDataAdapter sqlsel = new SqlDataAdapter("select * from WSFABB",conn);
  DataSet ABD = new DataSet();
  sqlsel.Fill(ABD );

this.DropDownList1.DataSource = ABD;
this.DropDownList1.DataTextField = "ABB002";
this.DropDownList1.DataValueField = "ABB001";
this.DropDownList1.DataBind();


引用 3 楼 taomanman 的回复:
C# code

SqlConnection conn = new SqlConnection("server=(local);user id=sa ;pwd=sa;database=WSFASP");
  conn.Open();
  SqlDataAdapter sqlsel = new SqlDataAdapter("select * from WSFABB",conn);
  D……
--------------------编程问答--------------------
this.DropDownList1.DataSource = ABD;
this.DropDownList1.DataTextField = "ABB002";
this.DropDownList1.DataValueField = "ABB001";
this.DropDownList1.DataBind();
--------------------编程问答--------------------

SqlConnection conn = new SqlConnection("server=(local);user id=sa ;pwd=sa;database=WSFASP");
  conn.Open();
  SqlDataAdapter sqlsel = new SqlDataAdapter("select * from WSFABB",conn);
  DataSet ABD = new DataSet();
  sqlsel.Fill(ABD );
this.DropDownList1.DataSource = ABD.Table[0];
this.DropDownList1.DataTextField = "ABB002";
this.DropDownList1.DataValueField = "ABB001";
this.DropDownList1.DataBind();
--------------------编程问答--------------------
 string sqlstring ="SELECT * from Employees";
               // SqlConnection MyConnection = new SqlConnection("server=PING-FE1EIQKYUZ\\WU;database=northwind;uid=sa;pwd=zd");
                SqlConnection MyConnection = new SqlConnection(connstr);
                SqlCommand MyCommand = new SqlCommand(sqlstring, MyConnection);

                MyConnection.Open();

                SqlDataReader dr = MyCommand.ExecuteReader();
                DropDownList5.DataSource = dr;
                DropDownList5.DataTextField = "City";
                DropDownList5.DataValueField = "EmployeeID";
                DropDownList5.DataBind();

                MyConnection.Close();
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,