Introduction
This very simple example provides a demonstration of how one can populate a TreeView
from a self-joined database table. In this example I have used a DataSet
containing one DataTable
that is populated with virtual XML
data.
Core
The SimpleDataSet
class provides a DataTable
populated with self-joined data. The SimpleTreeView
the actual data consumer object.
Sample of how the TreeView
is populated
public void CreateNodesOfParent(int iParent,TreeNode pNode) {
DataView dvwData = new DataView(sdsData.Tables[0]);
dvwData.RowFilter = "[PARENT] = " + iParent;
foreach(DataRowView Row in dvwData) {
if(pNode == null) {
TreeNode zNode = this.Nodes.Add(Row["Caption"].ToString());
CreateNodesOfParent(Int32.Parse(Row["ID"].ToString()),zNode);
} else {
TreeNode zNode = pNode.Nodes.Add(Row["Caption"].ToString());
CreateNodesOfParent(Int32.Parse(Row["ID"].ToString()),zNode);
}
}
}
Sample of how the DataTable
is populated
private void CreateDummyData() {
prvTable = new DataTable("tblTreeView");
prvTable.Columns.Add("ID",typeof(Int32));
prvTable.Columns.Add("PARENT",typeof(Int32));
prvTable.Columns.Add("CAPTION",typeof(String));
this.Tables.Add(prvTable);
prvTable.Rows.Add(new Object[] {1,0,"Countries"});
prvTable.Rows.Add(new Object[] {2,1,"Netherlands"});
prvTable.Rows.Add(new Object[] {3,2,"Cities"});
prvTable.Rows.Add(new Object[] {4,3,"The Hague"});
prvTable.Rows.Add(new Object[] {5,3,"Amsterdam"});
prvTable.Rows.Add(new Object[] {6,3,"Rotterdam"});
......
......
}
Gevik Babakhani is a software architect currently residing in The Netherlands. He started programming as hobby from the age of 12. His professional career began in 1997. Since then he works fulltime as a software architect. Currently he dedicates time and effort in Microsoft.NET.