DisplayTreeView: Part 2
The second part of DisplayTreeView does the following:
-
Iterates through each of the data rows in the Contacts data table.
-
If the value in a data row's ContactType column is “Personal”, it creates a contactNode and adds it to the subnodes of PersonalNode.
-
If the value in a data row's ContactType column is “Business”, it creates a contactNode and adds it to the subnodes of BusinessNode.
-
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.
-
For each contactNode, stores the ID value of the current data row in the node's Tag property.
-
It adds PersonalNode and BusinessNode as subnodes of treeView1.
-
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();
}