I want to display a tree view from database. I did using asp.net TreeView controller. But i cannot afford TreeView controller on my project. Hence i am trying to create tree view dynamically.
Code
using (SqlConnection Conn = new SqlConnection(connection))
{
string State = "Select * from IN_State";
string City = "Select * from IN_City";
string Treeview = State + ";" + City;
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(Treeview, Conn);
da.Fill(ds);
ds.Tables[0].TableName = "IN_State";
ds.Tables[1].TableName = "IN_City";
DataRelation dr = new DataRelation("StateCity", ds.Tables["IN_State"].Columns["S_Id"], ds.Tables["IN_City"].Columns["S_Id"]);
ds.Relations.Add(dr);
foreach (DataRow drState in ds.Tables["IN_State"].Rows)
{
TreeNode NDState = new TreeNode();
NDState.Text = drState["S_Name"].ToString();
NDState.Value = drState["S_Id"].ToString();
tview.Nodes.Add(NDState);
foreach (DataRow drCity in drState.GetChildRows("StateCity"))
{
TreeNode NDCity = new TreeNode();
NDCity.Text = drCity["C_Name"].ToString();
NDCity.Value = drCity["C_Id"].ToString();
NDState.ChildNodes.Add(NDCity);
}
}
}
Also tried using System.Web.UI.WebControls.TreeView