• 0

TreeView Control Question


Question

Does anyone know how to hierarchial data from a flat db table to a tree view. The problem I'm having is that some of the children are also parents and trying to recursively add these is proving had I have so far got my code to output the root elements, but I'm unsure how to proceed from there. Heres my code

  private void getCategories(int parent, int level)
  {
  	string query = "SELECT categoryid, name, parent FROM categories WHERE parent = '"+parent+"'";
  	listBox1.Items.Add(query);

  	NpgsqlCommand command = new NpgsqlCommand(query, this.db);

  	NpgsqlDataReader catReader = command.ExecuteReader();

  	while (catReader.Read())
  	{
    int parentid = Convert.ToInt32(catReader["parent"]);
    int categoryid = Convert.ToInt32(catReader["categoryid"]);

    if (parentid == 0)
    {
    	TreeNode rootNode = new TreeNode(Convert.ToString(catReader["name"]));
    	this.treeView1.Nodes.Add(rootNode);
    	this.getCategories(categoryid, level + 1);    	
    }
    else
    {
    	this.getCategories(categoryid, level + 1);
    }
  	}
  }

I'm using a Postgre database incase anyones wondering about the db connection stuff.

Link to comment
https://www.neowin.net/forum/topic/366060-treeview-control-question/
Share on other sites

2 answers to this question

Recommended Posts

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.