SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString.ToString());
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = this.GetData("Select distinct Pid, PName,cid,cname from TopParent");
this.PopulateTreeView(dt, 0, null);
BindRepeaterRight();
}
}
private void PopulateTreeView(DataTable dtParent, int parentId, TreeNode treeNode)
{
foreach (DataRow row in dtParent.Rows)
{
TreeNode child = new TreeNode
{
Text = row["cName"].ToString(),
Value = row["cId"].ToString()
};
if (parentId == 0)
{
TreeView2.Nodes.Add(child);
DataTable dtChild = this.GetData("Select * from TopParent where PId= " + child.Value);
PopulateTreeView(dtChild, int.Parse(child.Value), child);
// child.PopulateOnDemand = true;
child.SelectAction = TreeNodeSelectAction.SelectExpand;
// treeNode.ChildNodes.Add(child);
}
else
{
treeNode.ChildNodes.Add(child);
}
}
//DataSet ds = GetDataSet("select cid,cName from 'tablename where pname='0' and pId='" + strCompantid + "'");
//foreach (DataRow row in ds.Tables[0].Rows)
//{
// TreeNode node = new TreeNode();
// node.Text = row["DependencyName"].ToString();
// node.Value = row["Dependency"].ToString();
// node.PopulateOnDemand = true;
// node.SelectAction = TreeNodeSelectAction.SelectExpand;
// parent.ChildNodes.Add(node);
//}
}
private DataTable GetData(string query)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
return dt;
}
}
private void BindRepeaterRight()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM TopParent", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
reaptrright.DataSource = dt;
reaptrright.DataBind();
}
}
}
}
Suppose , i have a table in database. I want to bind The unique id and name in nodes and when i click on node , it must show the all data according to that id by expanding the nodes. How to do this.
Now Suppose when i click on India (its CID, Cid is qunique for each row ) it must be expand and show 3 child node as UP, HP, Kerala.