Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

sql - How to fill treeview and nodes according to parent in C#

I have a table in sql: table data goes like:

catid parentid catname
----- -------- -------------------
1     0        Operating Systems
2     1        Windows
3     1        Linux
4     5        Windows Server 2008 
5     2        Windows Server
6     1        Unix 

Parent id keeps its parent. For example; Windows Server 2008 comes first and belongs to Windows Server (its parent sometimes comes later).

In C# side i got that table in dataset, but could not solve rest.

for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
{
    TreeNode root = new TreeNode();
    TreeNode child = new TreeNode();
    if (ds.Tables[0].Rows[i]["parentid"] == 0)
    {

        root.Text = ds.Tables[0].Rows[i]["catname"];
        trview.Nodes.Add(root);
    }
    else
    {
        child.Text = ds.Tables[0].Rows[i]["catname"];
        root.ChildNodes.Add(child);                       
    }                             
}

How can i fill Treeview according to table. I could not solve how to design loop for it. If parentid is belong to a catid, it will be node of it.

Thank you for any advise or help

Kind Regards

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Iteration might not solve your problem. You can add all root-level nodes using iteration and then recursively add child nodes for each parent node. You can try the following:

private void BindTreeview()
{
    DataRow[] parentRows = mDataSet.Tables[0].Select("parentid = " + ROOT_PARENT_ID.ToString()); // # Get all root nodes

    foreach (DataRow row in parentRows) // # Add all root nodes to treeview and call for child nodes
    {
        TreeNode node = new TreeNode();
        node.Text = row["catname"].ToString();
        tvCategories.Nodes.Add(node);
        AddChildNodes((int)row["catid"], node);
    }
}

private void AddChildNodes(int catId, TreeNode node) // # Recursive method to add child nodes and call for child nodes of each child node
{
    DataRow[] childRows = mDataSet.Tables[0].Select("parentid = " + catId.ToString()); // # Get all child nodes of given node

    if (childRows.Length == 0) { return; } // # Recursion base case; if given node has no child nodes no more action is taken

    foreach (DataRow row in childRows)
    {
        TreeNode childNode = new TreeNode();
        childNode.Text = row["catname"].ToString();
        node.Nodes.Add(childNode);
        AddChildNodes((int)row["catid"], childNode);
    }
}

What it does: simple put; Finds root level categories and adds them to TreeView one by one, calling AddChildNodes method with added node and corresponding catid so that child nodes can be added. AddChildNodes method finds 1 level below childs of given catid and goes on with recursive calls for each found child until there are no more childs of a given node

This approach will work for your case, i just tested it. Just remember to replace:

  • mDataSet - with your DataSet instance
  • tvCategories - with your TreeView instance

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...