Skip to main content

DisplayTreeView: Part 2

The second part of DisplayTreeView does the following:

  1. Iterates through each of the data rows in the Contacts data table.

  2. If the value in a data row's ContactType column is “Personal”, it creates a contactNode and adds it to the subnodes of PersonalNode.

  3. If the value in a data row's ContactType column is “Business”, it creates a contactNode and adds it to the subnodes of BusinessNode.

  4. Initializes each contactNode by setting its label text to the Name value of the current data row. Each node's label text then contains the contact's name.

  5. For each contactNode, stores the ID value of the current data row in the node's Tag property.

  6. It adds PersonalNode and BusinessNode as subnodes of treeView1.

  7. Invokes EndUpdate on treeView1 so that its display can be painted.


 ...
 foreach (DataRow dr in ds.Tables["Contacts"].Rows)
 {
   if (dr["ContactType"].ToString().Equals("Personal"))
   {
     contactNode = new TreeNode(dr["Name"].ToString());
     contactNode.Tag = dr["ID"].ToString();
     contactNode.Name = "Name Node"; 
     PersonalNode.Nodes.Add(contactNode); 
   }
   if (dr["ContactType"].ToString().Equals("Business")) 
   {
     contactNode = new TreeNode(dr["Name"].ToString());
     contactNode.Tag = dr["ID"].ToString();
     contactNode.Name = "Name Node";
     BusinessNode.Nodes.Add(contactNode);
   }
  } 
   treeView1.Nodes.Add(PersonalNode);
   treeView1.Nodes.Add(BusinessNode);
   treeView1.EndUpdate(); 
}

FeedbackOpens in a new tab