views:

1043

answers:

2

Hello All,

I have a tree view control which I have to bind a dataset with multiple data tables in it, and have relations between them.

can you suggest how can I do that, I have tried many ways but none of them proved useful...

Thanks

A: 

Binding Data With ‘TreeView’ Control Asp.net 2.0 http://www.codeproject.com/KB/aspnet/DataTreeView.aspx

Robert Harvey
A: 

try this code, hope will be helpfull..I have just copy pasted code... you can change column name..

 DataTable dt=new DataTable();
    DataTable dt1 = new DataTable();
    DataSet ds = new DataSet();
    ds.Tables.Add(dt);
    ds.Tables.Add(dt1);
    ds.Relations.Add("children", dt.Columns["GSICCodeID"], dt1.Columns["GSICCodeID"]);
    if (ds.Tables[0].Rows.Count > 0)
    {
        tvSicCode.Nodes.Clear();
        Int32 i = 0;
        foreach (DataRow masterRow in ds.Tables[0].Rows)
        {
            TreeNode masterNode = new TreeNode((string)masterRow["Description"], Convert.ToString(masterRow["GSicCodeID"]));
            tvSicCode.Nodes.Add(masterNode);
            foreach (DataRow childRow in masterRow.GetChildRows("Children"))
            {
                TreeNode childNode = new TreeNode((string)childRow["SICCodeDesc"], Convert.ToString(childRow["SICCodeID"]));

                if (Convert.ToString(ds.Tables[1].Rows[i]["CarrierSICCode"]) != "")
                    childNode.Checked = true;

                masterNode.ChildNodes.Add(childNode);
                i++;
            }
        }

        tvSicCode.CollapseAll();
    }
Muhammad Akhtar
this is the right way but if I have more than two table then the size of code will be much larger, I was think of some ways that I create relation within the dataset then just bind that with the treeview is it possible??
Utkarsh
well, you can add more than 2 tables in Dataset and hence make relationship, but that I have not try what actually you want, but you can try....... I think you are trying to populate many level of treeview so I think you have to make recursive funtion to populate treeview.....
Muhammad Akhtar