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

DropDownList使用问题

怎么将读取数据库的数据付给DropDownList做数据源 --------------------编程问答-------------------- 除 --------------------编程问答-------------------- 普通控件满足不了的 --------------------编程问答--------------------
            //模拟从数据库获得的DataTable
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("Name", typeof(string));

            for (int i = 0; i < 10; i++)
            {
                DataRow dr = dt.NewRow();
                dr["ID"] = i;
                dr["Name"] = "Name:" + i;
                dt.Rows.Add(dr);
            }
            //绑定到DropDownList控件中
            DropDownList1.DataSource = dt;
            DropDownList1.DataTextField = "Name";
            DropDownList1.DataValueField = "ID";
            DropDownList1.DataBind();
--------------------编程问答--------------------

  //绑定到DropDownList控件中
            DropDownList1.DataSource = dt;//dt为数据集或list
            DropDownList1.DataTextField = "Test";
            DropDownList1.DataValueField = "Value";
            DropDownList1.DataBind();
--------------------编程问答--------------------  写一个查询方法,然后调用这个查询方法,在进行绑定

public     int  static  bindInfo()
{
   stringBulid  sb=new  stringBulid();
   sb.appline("select *  from  表名");
……
}

public void  getbindInfo()
{
    DataTable   dt =new  DataTable();
  dt=bindInfo();
   //绑定到DropDownList控件中             
DropDownList1.DataSource = dt;             
DropDownList1.DataTextField = "Name";             
DropDownList1.DataValueField = "ID";             
DropDownList1.DataBind();


}




--------------------编程问答-------------------- 数据源绑定啊 --------------------编程问答-------------------- 设置数据源绑定,在设置DisplayMember 和 ValueMember属性字段就可以了。 
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,