Ron's Sandbox

Dynamic TreeViews

Thursday, 7 May 2009 22:43 by hagermanr

First, create a data table that will hold our data for iteration.

DataTable temp = new DataTable();
col = new DataColumn("link_id");
temp.Columns.Add(col);
col = new DataColumn("link_parent");
temp.Columns.Add(col);
col = new DataColumn("link_name");
temp.Columns.Add(col);
col = new DataColumn("link_url");
temp.Columns.Add(col);
col = new DataColumn("link_description");
temp.Columns.Add(col);

temp.AcceptChanges();

string id = getID(N);

foreach (DataRow r1 in GetLinks().Tables[0].Rows)
{
    if (r1["link_parent"].ToString() == id)
    {
        DataRow r2 = temp.NewRow();
        r2["link_id"] = r1["link_parent"].ToString();
        r2["link_parent"] = r1["link_parent"].ToString();
        r2["link_name"] = r1["link_name"].ToString();
        r2["link_url"] = r1["link_url"].ToString();
        r2["link_description"] = r1["link_description"].ToString();

        temp.Rows.Add(r2);
        temp.AcceptChanges();
    }
}

ID (above) is the tree node being processed. So, we pass the current tree node to GetID() and that will return the ID of the record. We are comparing the node text to the current records link name.

private string getID(TreeNode N)
    {
        DataSet dsParent = new DataSet();

        dsParent = GetLinks();

        foreach (DataRow r in dsParent.Tables[0].Rows)
        {
            if (r["link_name"].ToString() == N.Text)
                return r["link_id"].ToString();
        }

        return "";
    }

Now we have a node with a link_id. Now we can iterate through the data set and look for child nodes.

foreach (DataRow r3 in temp.Rows)
{
    TreeNode tn = new TreeNode();
    tn.Text = r3["link_name"].ToString();
    tn.NavigateUrl = r3["link_url"].ToString();
    tn.ToolTip = r3["link_description"].ToString();
    tn.Value = r3["link_id"].ToString();

    initTreeView(tn);

    N.ChildNodes.Add(tn);

}

What we are doing here is we are looping through the records in the data table we created and populated above. We then create a new node for each record and call initTreeView() again passing it the new node. The child node is then added to the tree view control.

The last step is Page_Load().

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        tvwDefault.Nodes.Add(new TreeNode("Links"));
        tvwDefault.Nodes[0].Value = "-1";

        initTreeView(tvwDefault.Nodes[0]);
    }
}

 

If there wasn't a postback, i.e. submit hasn't been clicked, etc. then create a default treenode with the value of -1 since we know the database starts at 1. Then, initTreeView() and pass it our first node.

It is that simple.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Categories:   C Sharp
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading