• 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.
  • Posts

    • Clearly a lot of gamers. And I'm sorry but you are wrong, my 7800x3d (single ccd) offers huge benefits to me as a AAA 1440p gamer. And it's pretty dumb to crap on 1080p gamers as they make up like 90+% of steam gamers. But no, the x3d does not just offer benefits to esport gamers (one of the stupidest things I've seen in a while AND THATS SAYING SOMETHING.) Abd when I built my rig with my 7800x3d, I paired it with a RX7900xt and couldn't be happier.
    • Popular File Explorer alternative Files gets signature check, improved dual-pane, and more by Taras Buria A new update has arrived for File Preview, a feature-packed file manager for Windows 10 and 11. Version 3.9.14 introduces a useful new feature for file properties, dual-pane improvements, the ability to resize columns in the Columns View, and more. Starting with the Properties dialog, it now features a "Signatures" tab where you can view digital signatures associated with the file and their properties, such as version, issuer, expiry date, and more. You can also click the "Details" button to open Windows' native dialog. The dual-pane feature has been tweaked for a more intuitive experience. The "Add pane" command is now "Split pane." There is a new quick action for toggling dual-pane mode on or off, and settings now have a dedicated section for dual-pane mode. Finally, columns in the Columns View are now resizable, and search results now have a header with your current query. In addition to releasing the update, developers said the following regarding performance complaints from users: Files Preview is available in the Microsoft Store. You can purchase it there to support developers. Alternatively, you can get the app for free from the official website, and if you want to help developers with contributions or ideas, check out their Discord and GitHub.
    • So the iPhone only has a leading position in a few markets around the world: US, UK, UAE, couple of other smaller ones. As a result of the far more mixed eco-system Whatsapp is a lot more popular outside of the US than I think a lot of American's may realise. It's pretty much the messaging standard in the UK
  • Recent Achievements

    • Week One Done
      searchwayus earned a badge
      Week One Done
    • First Post
      kryptickid earned a badge
      First Post
    • First Post
      Nemesis-IV earned a badge
      First Post
    • First Post
      Aidan Helfrich earned a badge
      First Post
    • Collaborator
      bullgod69 earned a badge
      Collaborator
  • Popular Contributors

    1. 1
      +primortal
      760
    2. 2
      ATLien_0
      187
    3. 3
      +FloatingFatMan
      151
    4. 4
      Xenon
      117
    5. 5
      wakjak
      113
  • Tell a friend

    Love Neowin? Tell a friend!