Below code shows, how to generate nodes of treeview control dynamically.
<asp:TreeView ID="tree" runat="server" ForeColor="black" RootNodeStyle-ImageUrl="~/xpMyComp.gif" LeafNodeStyle-ImageUrl="~/help_page.gif"> </asp:TreeView>
C# Code
public void GenrateTreeView()
{
DataSet myDataSet = GetData();
foreach (DataRow parentRow in myDataSet.Tables["department"].Rows)
{
TreeNode parentNode = new TreeNode((string)parentRow["dname"]);
tree.Nodes.Add(parentNode);
foreach (DataRow childRow in parentRow.GetChildRows("Child"))
{
TreeNode childNode = new TreeNode((string)childRow["ename"]);
parentNode.ChildNodes.Add(childNode);
}
}
}
public DataSet GetData()
{
string strcon = System.Configuration.ConfigurationManager.AppSettings.Get("constring");
string empQuery = "SELECT * FROM employee ";
string deptQuery = "SELECT * FROM department";
SqlConnection con = new SqlConnection();
SqlDataAdapter da = new SqlDataAdapter(empQuery, strcon);
SqlDataAdapter da1 = new SqlDataAdapter(deptQuery, strcon);
DataSet myDataSet = new DataSet();
da.Fill(myDataSet, "employee");
da1.Fill(myDataSet, "department");
myDataSet.Relations.Add("Child", myDataSet.Tables["department"].Columns["deptno"],
myDataSet.Tables["employee"].Columns["deptno"]);
return myDataSet;
}