用父子关系数据填充下拉列表 [英] Fill dropdown with parent child relationship data
问题描述
使用父子关系数据填充下拉列表.使用递归函数
Fill dropdown with parent child relationship data.Use recursive function
推荐答案
<已移除id ="form1" runat ="server">
<removed id="form1" runat="server">
< asp:已移除id ="DropDownList1" runat =服务器" width ="200" onclick ="ShowHideTreeView()" xmlns:asp =#unknown">
<asp:removed id="DropDownList1" runat="server" width="200" onclick="ShowHideTreeView()" xmlns:asp="#unknown">
私有void FillDropdown()
{
DataTable数据=新的DataTable("data");
试试
{
数据= GetDataItem();
DataSet ds = new DataSet();
ds.Tables.Add(data.Copy());
ds.Relations.Add("rsParentChild",ds.Tables [0] .Columns ["id"],
ds.Tables [0] .Columns ["ParentId"],否);
foreach(ds.Tables [0] .Rows中的DataRow dr)
{
如果(int.Parse(dr ["ParentId"].ToString())== 0)
{
DropDownList1.Items.Add(new ListItem(dr ["Name"].ToString(),dr ["Id"].ToString()));
ListItem lstRoot =新的ListItem(dr ["Name"].ToString(),dr ["Id"].ToString());
PopulateTree(dr,lstRoot);
}
}
}
catch(ex ex例外)
{
}
}
私有void PopulateTree(DataRow dr,ListItem lstRoot)
{
foreach(dr.GetChildRows("rsParentChild")中的DataRow行)
{
ListItem lstChild = new ListItem(new String(''-'',(Int16.Parse(row ["HierarchyLevel"].ToString())-1)* 2)+ row ["Name"].ToString(),row ["Id"].ToString());
DropDownList1.Items.Add(lstChild);
PopulateTree(row,lstChild);
}
}
private void FillDropdown()
{
DataTable data = new DataTable("data");
try
{
data = GetDataItem();
DataSet ds = new DataSet();
ds.Tables.Add(data.Copy());
ds.Relations.Add("rsParentChild", ds.Tables[0].Columns["id"],
ds.Tables[0].Columns["ParentId"], false);
foreach (DataRow dr in ds.Tables[0].Rows)
{
if (int.Parse(dr["ParentId"].ToString()) == 0)
{
DropDownList1.Items.Add(new ListItem(dr["Name"].ToString(), dr["Id"].ToString()));
ListItem lstRoot = new ListItem(dr["Name"].ToString(), dr["Id"].ToString());
PopulateTree(dr, lstRoot);
}
}
}
catch (Exception ex)
{
}
}
private void PopulateTree(DataRow dr, ListItem lstRoot)
{
foreach (DataRow row in dr.GetChildRows("rsParentChild"))
{
ListItem lstChild = new ListItem(new String(''-'', (Int16.Parse(row["HierarchyLevel"].ToString()) - 1)*2) + row["Name"].ToString(), row["Id"].ToString());
DropDownList1.Items.Add(lstChild);
PopulateTree(row, lstChild);
}
}
这篇关于用父子关系数据填充下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!