0

I have a data table having columns topicid,topic name,category id,category name,subcategory id,sub category name i have to bind this table to to tree view so that Topic names will appear as parent node,category name as child node of topics and subcategory name will come as child node of category.

All the nodes having name as id fields example topic node has Topic name and Topic id

category node as category name and category id and sub category node has subcategory name and id.

Akki0007
  • 87
  • 3
  • 8

1 Answers1

0

here is code...

DataTable dtbl1=new DataTable();//parent datatable
DataTable dtbl2=new DataTable();//child datatable

DataSet ds = new DataSet();
ds.Tables.Add(dtbl1);
ds.Tables.Add(dtbl2);
ds.Relations.Add("Children", dtbl1.Columns["dtb1ID"], dtbl2.Columns["dtbl2ID"]);//define parent child relation in dataset

if (ds.Tables[0].Rows.Count > 0)
{
    trv.Nodes.Clear();
    Int32 count = 0;

    foreach(DataRow masterRow in  ds.Tables[0].Rows)
    {
        TreeNode masterNode = new TreeNode((String)masterRow["dtbl1ColumnYouWantToDisplay"], Convert.ToString(masterRow["dtbl1ID"]));
        trv.Nodes.Add(masterNode);

        foreach (DataRow childRow in masterRow.GetChildRows("Children"))
        {
            TreeNode childNode = new TreeNode((String)childRow["dtbl2ColumnYouWantToDisplay"], Convert.ToString(childRow["dtb2ID"]));
            masterNode.ChildNodes.Add(childNode);
            count++;
        }
    }
    trv.ExpandAll();
}
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191