Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem to update hierarchical data and display it in treeView.

Problem to update hierarchical data and display it in treeView.

Scheduled Pinned Locked Moved C#
databaseperformancehelptutorialannouncement
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hdv212
    wrote on last edited by
    #1

    Hi i have a simple app which display emplyees organization chart from northwind database. here is my code to accomplish this :

    private void btnLoadData_Click(object sender, EventArgs e)
    {
    this.treeView1.Nodes.Clear();

      this.\_dtEmployees = this.GetEmployees();
      this.FillHieraricalData(this.\_dtEmployees, this.treeView1, "LastName", "EmployeeID", "ReportsTo");
      this.treeView1.ExpandAll();
    

    }

    private DataTable GetEmployees()
    {
    DataTable dt = new DataTable();

      using (SqlConnection con = new SqlConnection(Properties.Settings.Default.NorthwindConnectionString))
      {
        SqlCommand cmd = con.CreateCommand();
        cmd.CommandText = "select \* from employees";
        con.Open();
        dt.Load(cmd.ExecuteReader());
        con.Close();
      }
    
      return dt;
    

    }

    private void FillHieraricalData(DataTable dt, TreeView treeView, string infoColumnName,
    string pkColumnName, string parentColumnName)
    {
    TreeNode node = null;
    foreach (DataRow row in dt.Rows)
    {
    node = new TreeNode(row[infoColumnName].ToString());
    node.Tag = row[pkColumnName];

        if (row\[parentColumnName\] != DBNull.Value)
        {
          List<TreeNode> foundNodes = new List<TreeNode>();
    
          // search over treeView nodes to found parent of row (if exists)
          SearchNodeInTreeView(treeView.Nodes, Convert.ToInt32(row\[parentColumnName\]), foundNodes);
          if (foundNodes.Count > 0)
            foundNodes\[0\].Nodes.Add(node);
        }
        else // if row parent does not exists, then it's parent itself (or it's root), it should add to level 0 of treeView
          treeView.Nodes.Add(node);
      }
    

    }

    // this method search in treeView.Nodes to find specific nodes base on 'value'
    private void SearchNodeInTreeView(TreeNodeCollection nodesToSearch, int value, List<TreeNode> foundNodes)
    {
    for (int i = 0; i < nodesToSearch.Count; i++)
    {
    if (Convert.ToInt32(nodesToSearch[i].Tag) == value)
    foundNodes.Add(nodesToSearch[i]);

        //Recursively search in the child nodes        
        SearchNodeInTreeView(nodesToSearch\[i\].Nodes, value, foundNodes);
      }
    

    }

    However, when i update data in memory (via dataTable) and redisplay data in treeView, the row that has been modfied has been removed from treeView and not display. for example, when i change 'Reports To' of any employee, that employee does not display in treeView! here

    M 1 Reply Last reply
    0
    • H hdv212

      Hi i have a simple app which display emplyees organization chart from northwind database. here is my code to accomplish this :

      private void btnLoadData_Click(object sender, EventArgs e)
      {
      this.treeView1.Nodes.Clear();

        this.\_dtEmployees = this.GetEmployees();
        this.FillHieraricalData(this.\_dtEmployees, this.treeView1, "LastName", "EmployeeID", "ReportsTo");
        this.treeView1.ExpandAll();
      

      }

      private DataTable GetEmployees()
      {
      DataTable dt = new DataTable();

        using (SqlConnection con = new SqlConnection(Properties.Settings.Default.NorthwindConnectionString))
        {
          SqlCommand cmd = con.CreateCommand();
          cmd.CommandText = "select \* from employees";
          con.Open();
          dt.Load(cmd.ExecuteReader());
          con.Close();
        }
      
        return dt;
      

      }

      private void FillHieraricalData(DataTable dt, TreeView treeView, string infoColumnName,
      string pkColumnName, string parentColumnName)
      {
      TreeNode node = null;
      foreach (DataRow row in dt.Rows)
      {
      node = new TreeNode(row[infoColumnName].ToString());
      node.Tag = row[pkColumnName];

          if (row\[parentColumnName\] != DBNull.Value)
          {
            List<TreeNode> foundNodes = new List<TreeNode>();
      
            // search over treeView nodes to found parent of row (if exists)
            SearchNodeInTreeView(treeView.Nodes, Convert.ToInt32(row\[parentColumnName\]), foundNodes);
            if (foundNodes.Count > 0)
              foundNodes\[0\].Nodes.Add(node);
          }
          else // if row parent does not exists, then it's parent itself (or it's root), it should add to level 0 of treeView
            treeView.Nodes.Add(node);
        }
      

      }

      // this method search in treeView.Nodes to find specific nodes base on 'value'
      private void SearchNodeInTreeView(TreeNodeCollection nodesToSearch, int value, List<TreeNode> foundNodes)
      {
      for (int i = 0; i < nodesToSearch.Count; i++)
      {
      if (Convert.ToInt32(nodesToSearch[i].Tag) == value)
      foundNodes.Add(nodesToSearch[i]);

          //Recursively search in the child nodes        
          SearchNodeInTreeView(nodesToSearch\[i\].Nodes, value, foundNodes);
        }
      

      }

      However, when i update data in memory (via dataTable) and redisplay data in treeView, the row that has been modfied has been removed from treeView and not display. for example, when i change 'Reports To' of any employee, that employee does not display in treeView! here

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      You are putting the row id into the tag property

      node.Tag = row[pkColumnName];

      But I don't see where you find that row to update the record when editing. You may be updating row[0] all the time. I have been known to stick the entire row object into the tag property.

      node.Tag = row;

      Never underestimate the power of human stupidity RAH

      H 1 Reply Last reply
      0
      • M Mycroft Holmes

        You are putting the row id into the tag property

        node.Tag = row[pkColumnName];

        But I don't see where you find that row to update the record when editing. You may be updating row[0] all the time. I have been known to stick the entire row object into the tag property.

        node.Tag = row;

        Never underestimate the power of human stupidity RAH

        H Offline
        H Offline
        hdv212
        wrote on last edited by
        #3

        Thanks Mycroft but still i have the same problem.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups