I am trying to create a TreeView control that works with an ADO Dataset
DataTable or the new BindingSource stuff in .NET 2.0 to build a Treeview
that is populated.
This is what I came up with so far... I can't run it yet because I don't
have the DataTable source... I still want to get some sort of assessment as
to whether I am heading in the right direction. Here is the code:
================================
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication1
{
class NewTree:System.Windows.Forms.TreeView
{
// the data used to build the treeview
protected System.Data.DataTable m_Table;
private System.Data.DataColumn m_KeyColumn;
private System.Data.DataColumn m_ParentKeyColumn;
private System.Data.DataColumn m_TextKeyColumn;
private System.Windows.Forms.TreeNode m_TreeNode;
// Constructor for the class
public NewTree(DataTable m_Table,
string m_KeyColumnName, //name of the primary key column
string m_ParentKeyColumnName, //name of the parent id column
string m_TextColumnName, // name of the column used for text.
int m_StartValue)
{
// Initialize references to columns needed
m_KeyColumn = m_Table.Columns[m_KeyColumnName];
m_ParentKeyColumn = m_Table.Columns[m_ParentKeyColumnName];
m_TextKeyColumn = m_Table.Columns[m_TextColumnName];
// Set initial table filter
string m_filterClause;
m_filterClause = m_ParentKeyColumnName + " = " + m_StartValue.ToString();
m_Table.Select(m_filterClause);
// turn off screen refresh
this.BeginUpdate();
// add relevant root nodes
foreach (DataRow m_thisRow in m_Table.Rows)
{
m_TreeNode = new TreeNode();
m_TreeNode.Tag = m_thisRow.ItemArray.GetValue(m_KeyColumn.Ordinal() );
m_TreeNode.Text = m_thisRow.ItemArray.GetValue(m_TextKeyColumn.Ordin al());
// recursive data function should build a full branch of nodes
buildBranch(m_TreeNode);
this.Nodes.Add(m_TreeNode);
}
// turn on screen refresh
this.EndUpdate();
}
/// <summary>
/// buildBranch is an attempt to build a recursive function to
/// build nodes for a treeview control.
/// I am making the assumption that as a object m_thisNode gets passed
/// by reference. Thus the parent level object should be modified with
/// all its children by the time the root node is added to the treeview
/// nodes collection.
/// </summary>
/// <param name="m_thisNode"></param>
private void buildBranch(TreeNode m_thisNode)
{
TreeNode m_InnerNode;
// set up new filter for table
m_filterClause = m_ParentKeyColumnName + " = " + m_thisNode.Tag.ToString();
m_Table.Select(m_filterClause);
// add child nodes
foreach (DataRow m_thisRow in m_Table.Rows)
{
m_InnerNode = new TreeNode();
m_InnerNode.Tag = m_thisRow.ItemArray.GetValue(m_KeyColumn.Ordinal() );
m_InnerNode.Text = m_thisRow.ItemArray.GetValue(m_TextKeyColumn.Ordin al());
buildBranch(m_InnerNode);
m_thisNode.Nodes.Add(m_InnerNode);
}
}
}
}
==============================
When I dragged the dataset into the application I expected to see references
to a DataSet and DataTables. instead I am seeing BindSources and stuff. I
am not sure how to translate back to the DataTable in order to even test
this.
Your suggestions are welcome.