有4个字段 name  unit address telpone 如何手动遍历添加到DropDownList里
我知道是用DropDownList.Items.Add 方法,但具体怎么写不知道了 

解决方案 »

  1.   


    dropdownlist.Items.Add(new ListItem("name","nameId")
    ...
      

  2.   

    用foreach循环
    foreach(string str in 字段)
    {
    DropDownList.Items.Add(str);
    }
      

  3.   

    一个DropDownList里面放四个字段打算怎么显示?
      

  4.   

    dropdownlist.Items.Add(new ListItem("name","nameId")
      

  5.   

    不知道楼主是不是想ADD到页面的所有DropDownList
      

  6.   


    DataTable dt = new DataTable();
    dt=这里把你返回的table放进去
    for (int i = 0; i < dt.Rows.Count; i++)
    {
       ListItem lt = new ListItem();
       lt.Text = dt.Rows[i]["name"].ToString();
       lt.Value = dt.Rows[i]["name"].ToString();
       lt.Attributes["unit"] = dt.Rows[i]["unit"].ToString();
       lt.Attributes["address"] = dt.Rows[i]["address"].ToString();
       lt.Attributes["telpone"] = dt.Rows[i]["telpone"].ToString();
       dropdownlist.Items.Add(lt);
    }
      

  7.   


    这样不行啊dt.Rows.Count是0,lt.Attributes["unit"] = dt.Rows[i]["unit"].ToString();也报错
      

  8.   

    如果你从数据库查询出来的的字段,放到DataSet里面。
    还可以这样做.
    DataSet ds = new DataSet();this.DropDownList1.DataSource =ds.Tables[0];
    this.DropDownList1.DataValueField = "字段";//显示的字段
    this.DropDownList1.DataTextField = "字段";//value值的字段
    this.DropDownList1.DataBind();
      

  9.   

    绑定的方法我已经知道了 我用绑定的方法绑到DropDownList,怎样才能显示出字段名啊 例如 name    unit     address  telpone
             小红    小小公园     广州     2345678
      

  10.   

    这个方法不一定是最好的~1
    先把查询字段重新构建 datatable,datarow.
    把列替换为行。
    再绑定.
    应该还有更加好的方法