定义一个私有变量,要求有ID和NAME两个字段:DataTable depttable;建立列表(string 上级ID,string 前缀) { DataView table = DataObj.GetDataView("Select ID,前缀+NAME from Dept where belongid=上级ID Order By ID"); foreach(DataRowView drv in table { depttable.ImportRow(drv.Row); 建立列表(drv["ID"].ToString(),前缀+"|--"); } }//能过上面方法,把列表信息加到depttable后,把这张表绑定到你的下拉列表框应该就可以了
DropDownList1.DataSource=depttable; DropDownList1.DataValueField="ID"; DropDownList1.DataTextField="Name";上面的SQL语句要改一下: Select ID,前缀+NAME AS Name from Dept where belongid=上级ID Order By ID
因为我是刚学的, 所以绑定还不太清楚,还不能做到上面第一点那样,麻烦有没有代码发一段给你看一看, 谢谢!
这相对象名可以
class item
{
int id
string text
}
item tmp = new item()
tmp.id = xx;
tmp.text =xx;
.items.add(tmp)
{
int id;
string text;
public override string ToString()
{
return text;
}
}
加一个重载tostring()
1、DropDownList.Items.Add()方法有2次重载,应用参数是listitem的方法,就可以设定值和要显示的不同了。你先通过dataset构建一个listitem实例,然后在加上去。2、因为在你的dataset里面有多张表,所以用每个表构建树的每一层节点。
{
DataView table = DataObj.GetDataView("Select ID,前缀+NAME from Dept where belongid=上级ID Order By ID");
foreach(DataRowView drv in table
{
depttable.ImportRow(drv.Row);
建立列表(drv["ID"].ToString(),前缀+"|--");
}
}//能过上面方法,把列表信息加到depttable后,把这张表绑定到你的下拉列表框应该就可以了
DropDownList1.DataValueField="ID";
DropDownList1.DataTextField="Name";上面的SQL语句要改一下:
Select ID,前缀+NAME AS Name from Dept where belongid=上级ID Order By ID
你就想怎么显示就怎么显示了