日期:2014-05-18  浏览次数:20433 次

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

------解决方案--------------------
C# code

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

------解决方案--------------------
用foreach循环
foreach(string str in 字段)
{
DropDownList.Items.Add(str);
}
------解决方案--------------------
一个DropDownList里面放四个字段打算怎么显示?
------解决方案--------------------
dropdownlist.Items.Add(new ListItem("name","nameId")

------解决方案--------------------
不知道楼主是不是想ADD到页面的所有DropDownList
------解决方案--------------------
C# code

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

------解决方案--------------------
如果你从数据库查询出来的的字段,放到DataSet里面。
还可以这样做.
DataSet ds = new DataSet();

this.DropDownList1.DataSource =ds.Tables[0];
this.DropDownList1.DataValueField = "字段";//显示的字段
this.DropDownList1.DataTextField = "字段";//value值的字段
this.DropDownList1.DataBind();
------解决方案--------------------
这个方法不一定是最好的~1
先把查询字段重新构建 datatable,datarow.
把列替换为行。
再绑定.
应该还有更加好的方法