|
-
May 22nd, 2009, 06:06 PM
#1
[2.0+] Reading/Writing TreeView Node Structures
VB Version Here
I've seen many people use TreeViews for the purpose of creating and storing a structure. The following is code that will let you load a DataTable into a TreeView and also write a TreeView's nodes to a DataTable. There are 5 methods to this, of those, only 2 are called by you. The other 3 are for the recursion. I have contained all these methods in a class I called TreeViewrw.
Function #1: ToTable - Creates a DataTable from the nodes in the passed TreeView.
csharp Code:
public DataTable ToTable(TreeView tv) { DataTable dt = new DataTable("TreeNodes"); int id = 1; dt.Columns.Add("ParentID", typeof(int)); dt.Columns.Add("NodeID", typeof(int)); dt.Columns.Add("Name", typeof(string)); foreach (TreeNode pn in tv.Nodes) { dt.Rows.Add(0, id, pn.Name); this.GetAllNodes(ref dt, pn, id, ref id); id += 1; } return dt; } private void GetAllNodes(ref DataTable dt, TreeNode pn, int parentid, ref int id) { foreach (TreeNode cn in pn.Nodes) { id += 1; dt.Rows.Add(parentid, id, cn.Name); this.GetAllNodes(ref dt, cn, id, ref id); } }
Examples of use are at the bottom.
Function #2: FromTable - Reads the passed DataTable and returns an Array of TreeNodes representing the DataTable's contents.
Code:
public TreeNode[] FromTable(DataTable dt, string parent, string node, string name)
{
List<TreeNode> tnodes = new List<TreeNode>();
string[] columns = new string[3] {parent, node, name};
if (dt.Columns.Contains(parent) && dt.Columns.Contains(node) && dt.Columns.Contains(name))
{
dt.DefaultView.RowFilter = "[" + parent + "] = 0";
foreach (DataRowView r in dt.DefaultView)
{
tnodes.Add(this.ReadNodes(columns, r, dt));
}
return tnodes.ToArray();
}
else
{
return tnodes.ToArray();
}
}
private TreeNode ReadNodes(string[] columns, DataRowView r, DataTable dt)
{
TreeNode cn = new TreeNode();
cn.Name = r[columns[2]].ToString();
cn.Text = cn.Name;
this.GetAllNodes(columns, ref cn, dt, (int)r[columns[1]]);
return cn;
}
private void GetAllNodes(string[] columns, ref TreeNode pn, DataTable dt, int nodeid)
{
dt.DefaultView.RowFilter = "[" + columns[0].ToString() + "] = " + nodeid.ToString();
foreach (DataRowView r in dt.DefaultView)
{
TreeNode cn = new TreeNode();
cn.Name = r[columns[2]].ToString();
cn.Text = cn.Name;
pn.Nodes.Add(cn);
this.GetAllNodes(columns, ref cn, dt, (int)r[columns[1]]);
}
}
Examples of use are at the bottom.
Examples:
ToTable
For this function there is 1 parameter, simple enough. You pass the TreeView that you want a DataTable representation of it's nodes and it returns it.
Use:
csharp Code:
private void button2_Click(object sender, EventArgs e) { TreeViewRW tvrw = new TreeViewRW(); this.dataGridView1.DataSource = tvrw.ToTable(this.treeView1); }
Doesn't get much simpler then that. So assuming you had button1, datagridview1 and treeview1 on the form, and treeview1's nodes looked like this:

Clicking that button would yield this:

FromTable
For this function there are 4 parameters:
The first (dt) is the DataTable containing the data to read.
The second (parent) is the column name in the DataTable that contains what is called the ParentID.
The third (node) is the column name in the DataTable that contains the unique NodeID.
The fourth (name) is the column name in the DataTable that contains the value that will be assigned to both the node's name and text properties.
Use:
csharp Code:
private void button1_Click(object sender, EventArgs e) { TreeViewRW tvrw = new TreeViewRW(); DataTable dt = new DataTable("TreeNodes"); dt.Columns.Add("ParentID", typeof(int)); dt.Columns.Add("NodeID", typeof(int)); dt.Columns.Add("Name", typeof(string)); dt.Rows.Add(0, 1, "Parent 1"); dt.Rows.Add(0, 2, "Parent 2"); dt.Rows.Add(1, 3, "Child 1"); dt.Rows.Add(1, 4, "Child 2"); dt.Rows.Add(3, 5, "Child 3"); dt.Rows.Add(2, 6, "Child 4"); this.treeView1.Nodes.AddRange(tvrw.FromTable(dt, "ParentID", "NodeID", "Name")); }
So as you can see I created a small sample DataTable to show how the data is read. Just by looking at the rows it's easy to tell that Child 1's parent is Parent 1, Child 3's parent is Child 1, etc... Assuming you had button1 and treeview1 on the form, clicking the button would yield this:

Here is the class:
Code:
public class TreeViewRW
{
public DataTable ToTable(TreeView tv)
{
DataTable dt = new DataTable("TreeNodes");
int id = 1;
dt.Columns.Add("ParentID", typeof(int));
dt.Columns.Add("NodeID", typeof(int));
dt.Columns.Add("Name", typeof(string));
foreach (TreeNode pn in tv.Nodes)
{
dt.Rows.Add(0, id, pn.Name);
this.GetAllNodes(ref dt, pn, id, ref id);
id += 1;
}
return dt;
}
public TreeNode[] FromTable(DataTable dt, string parent, string node, string name)
{
List<TreeNode> tnodes = new List<TreeNode>();
string[] columns = new string[3];
columns[0] = parent;
columns[1] = node;
columns[2] = name;
if (!dt.Columns.Contains(parent) && !dt.Columns.Contains(node) && !dt.Columns.Contains(name))
{
return tnodes.ToArray();
}
else
{
dt.DefaultView.RowFilter = "[" + parent + "] = 0";
foreach (DataRowView r in dt.DefaultView)
{
tnodes.Add(this.ReadNodes(columns, r, dt));
}
return tnodes.ToArray();
}
}
private TreeNode ReadNodes(string[] columns, DataRowView r, DataTable dt)
{
TreeNode cn = new TreeNode();
cn.Name = r[columns[2]].ToString();
cn.Text = cn.Name;
this.GetAllNodes(columns, ref cn, dt, (int)r[columns[1]]);
return cn;
}
private void GetAllNodes(ref DataTable dt, TreeNode pn, int parentid, ref int id)
{
foreach (TreeNode cn in pn.Nodes)
{
id += 1;
dt.Rows.Add(parentid, id, cn.Name);
this.GetAllNodes(ref dt, cn, id, ref id);
}
}
private void GetAllNodes(string[] columns, ref TreeNode pn, DataTable dt, int nodeid)
{
dt.DefaultView.RowFilter = "[" + columns[0].ToString() + "] = " + nodeid.ToString();
foreach (DataRowView r in dt.DefaultView)
{
TreeNode cn = new TreeNode();
cn.Name = r[columns[2]].ToString();
cn.Text = cn.Name;
pn.Nodes.Add(cn);
this.GetAllNodes(columns, ref cn, dt, (int)r[columns[1]]);
}
}
}
Last edited by ForumAccount; May 22nd, 2009 at 06:38 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|