Click here to Skip to main content
15,889,509 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Fill dropdown with parent child relationship data.Use recursive function
Posted
Updated 8-Apr-10 3:21am
v2

1 solution

<removed id="form1" runat="server">

<asp:removed id="DropDownList1" runat="server" width="200" onclick="ShowHideTreeView()" xmlns:asp="#unknown">





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);
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900