Populating WinForms Controls at Runtime with C#


Populate a WinForms TreeView at Runtime Using C#

The TreeView control displays labelled items in a hierarchy, like this:

A Simple WinForms TreeView

ComboBox screenshot

The C# class below fills a WinForms TreeView at runtime using a DataTable.

Create a new class file called TreeViewRenderer.cs and paste the code below into it. Replace the yourNamespace placeholder with you own project's namespace.

UML Class Diagram

UML Class Diagram

using System;
using System.Windows.Forms;
using System.Data;
 
namespace yourNamespace
{
 public class TreeViewRenderer
 {
  TreeView treeView;
  DataTable dataTable;
  int keyValue = 0;
  int displayValue = 1;
  
  public TreeViewRenderer(TreeView treeView,
    DataTable dataTable)
  {
   this.treeView = treeView;
   this.dataTable = dataTable;
   treeView.Nodes.Clear();
  }
  
  public void PopulateControlWithChild(DataTable dataTableChild) {
  	treeView.BeginUpdate();
   foreach(DataRow dr in dataTable.Rows) {
    TreeNode t = new TreeNode();
    t.Tag = dr[keyValue].ToString();
    t.Text = dr[displayValue].ToString();
    treeView.Nodes.Add(t);
    foreach(DataRow drC in dataTableChild.Rows) {
     if (drC[0].ToString() == dr[0].ToString()) {
      treeView.Nodes[t.Index].Nodes.Add(drC[1].ToString(),drC[2].ToString()); 
     }
    }  
   }
   treeView.EndUpdate();
  }
  
  public void PopulateControl() {
  	treeView.BeginUpdate();
   foreach(DataRow dr in dataTable.Rows) {
    TreeNode t = new TreeNode();
    t.Tag = dr[keyValue].ToString();
    t.Text = dr[displayValue].ToString();
    treeView.Nodes.Add(t);
    for(int x = 2; x < dataTable.Columns.Count; x++) {
     treeView.Nodes[t.Index].Nodes.Add(dr[x].ToString());
    }    
   }
   treeView.EndUpdate();
  }  
 }
}
 

This class contains two methods: PopulateControl() displays data from a single DataTable. This is designed to display all fields from a data source in a hierarcical structure. In this method the first column of the DataTable is the Tag, the second is the Text and the rest are displayed when the node is expanded.

To use the TreeViewRenderer PopulateControl() method in your C# code, drag and drop a TreeView onto your Windows form, instantiate a new object in the form's code, providing the name of your TreeView and your DataTable in the method's arguments and call the new object's PopulateControl method, like this:

TreeViewRenderer tvR = new TreeViewRenderer(<myTreeView>, <myDataTable>);
tvR.PopulateControl();

Place the above snippet in the form's constructor, load event or other method. You're now ready to create and populate a DataTable to pass to the TreeViewRenderer class and populate the WinForms TreeView.

The second class method, PopulateControlWithChild() takes an additional DataTable as an argument and displays data in an expanded Node where its values match those of the DataTable given to the constructor.

As an example, assume the DataTables were populated by the following two SQL SELECT queries. Where the first DataTable given to the constructor has this structure:

SELECT
	PARENT_ID, PARENT_TEXT
FROM
	PARENT_DATA

The second query, provided to the PopulateControlWithChild() method, should have this structure:

SELECT
	PARENT_ID, CHILD_ID, CHILD_TEXT
FROM
	CHILD_DATA

Where the queries share a value, in this case the PARENT_ID, the method will display values from the CHILD_DATA hierarchically under the parent. The shared data item would normally be a RDBMS table FOREIGN KEY. This example class requires that the related data item is the first field in both SELECT clauses.

To use the TreeViewRenderer PopulateControlWithChild() method in your C# code, drag and drop a TreeView onto your Windows form, instantiate a new object in the form's code, providing the name of your TreeView and your DataTable in the method's arguments and call the new object's PopulateControl method, like this:

TreeViewRenderer tvR = new TreeViewRenderer(<myTreeView>, <myParentDataTable>);
tvR.PopulateControlWithChild(<myChildDataTable>);

Here's an example using the above SELECT clauses illustrated above and the GetData() method, (discussed here):

string sql = "SELECT PARENT_ID, PARENT_TEXT FROM PARENT_DATA";
string childSQL = "SELECT PARENT_ID, CHILD_ID, CHILD_TEXT FROM CHILD_DATA";
TreeViewRenderer tvR = new TreeViewRenderer(treeView1, GetData(sql));
tvR.PopulateControlWithChild(GetData(childSQL));

Place the above snippet in the form's constructor, load event or other method. You're now ready to create and populate a DataTable to pass to the TreeViewRenderer class and populate the WinForms TreeView.


Next Topic