DBTreeView
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
internal class DBTreeView : TreeView
{
private object rootValue;
private string parentMember;
private string childMember;
private string textMember;
private object dataSource;
private CurrencyManager cm;
public object DataSource
{
get { return dataSource; }
set
{
if (value == null)
{
this.cm = null;
//this.GroupingChanged();
}
else
{
if (!((value is IList) | (value is IListSource)))
{
throw new Exception("Invalid DataSource");
}
if (value is IListSource)
{
IListSource myListSource = (IListSource)value;
if (myListSource.ContainsListCollection)
{
throw new Exception("Invalid DataSource");
}
this.dataSource = value;
this.cm = (CurrencyManager)this.BindingContext[value];
//this.GroupingChanged();
}
else
{
this.dataSource = value;
this.cm = (CurrencyManager)this.BindingContext[value];
//this.GroupingChanged();
}
}
}
}
[Category("Data")]
public string TextMember
{
get { return textMember; }
set { textMember = value; }
}
[Category("Data")]
public string ChildMember
{
get { return childMember; }
set { childMember = value; }
}
[Category("Data")]
public object RootValue
{
get { return this.rootValue; }
set { this.rootValue = value; }
}
[Category("Data")]
public string ParentMember
{
get
{
return this.parentMember;
}
set
{
this.parentMember = value;
}
}
public void BuildTree()
{
this.Nodes.Clear();
this.BuildTree(this.Nodes, this.rootValue);
}
private void BuildTree(TreeNodeCollection currNodes, object parentValue)
{
if ((this.cm != null) && (this.cm.List != null))
{
IBindingListView bindingList = this.cm.List as IBindingListView;
if (bindingList != null && bindingList.SupportsFiltering)
{
bindingList.Filter = string.Format("{0}='{1}'", this.parentMember, parentValue);
}
IList innerList = this.cm.List;
int currentListIndex = 0;
PropertyDescriptorCollection properties = this.cm.GetItemProperties();
PropertyDescriptor pdParentMember = properties[this.parentMember];
PropertyDescriptor pdChildMember = properties[this.childMember];
PropertyDescriptor pdTextMember = properties[this.textMember];
//TreeNodeCollection currNodes = this.Nodes;
while (currentListIndex < innerList.Count)
{
object currObject = innerList[currentListIndex];
TreeLeafNode myFirstNode = new TreeLeafNode();
myFirstNode.Text = Convert.ToString(pdChildMember.GetValue(currObject));
myFirstNode.Value = pdChildMember.GetValue(currObject);
myFirstNode.Item = currObject;
//this.BuildTree(myFirstNode.Nodes, pdChildMember.GetValue(currObject));
currNodes.Add(myFirstNode);
currentListIndex += 1;
}
for (int i = 0; i < currNodes.Count; i++)
{
TreeLeafNode node = currNodes[i] as TreeLeafNode;
this.BuildTree(node.Nodes, node.Value);
}
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
internal class DBTreeView : TreeView
{
private object rootValue;
private string parentMember;
private string childMember;
private string textMember;
private object dataSource;
private CurrencyManager cm;
public object DataSource
{
get { return dataSource; }
set
{
if (value == null)
{
this.cm = null;
//this.GroupingChanged();
}
else
{
if (!((value is IList) | (value is IListSource)))
{
throw new Exception("Invalid DataSource");
}
if (value is IListSource)
{
IListSource myListSource = (IListSource)value;
if (myListSource.ContainsListCollection)
{
throw new Exception("Invalid DataSource");
}
this.dataSource = value;
this.cm = (CurrencyManager)this.BindingContext[value];
//this.GroupingChanged();
}
else
{
this.dataSource = value;
this.cm = (CurrencyManager)this.BindingContext[value];
//this.GroupingChanged();
}
}
}
}
[Category("Data")]
public string TextMember
{
get { return textMember; }
set { textMember = value; }
}
[Category("Data")]
public string ChildMember
{
get { return childMember; }
set { childMember = value; }
}
[Category("Data")]
public object RootValue
{
get { return this.rootValue; }
set { this.rootValue = value; }
}
[Category("Data")]
public string ParentMember
{
get
{
return this.parentMember;
}
set
{
this.parentMember = value;
}
}
public void BuildTree()
{
this.Nodes.Clear();
this.BuildTree(this.Nodes, this.rootValue);
}
private void BuildTree(TreeNodeCollection currNodes, object parentValue)
{
if ((this.cm != null) && (this.cm.List != null))
{
IBindingListView bindingList = this.cm.List as IBindingListView;
if (bindingList != null && bindingList.SupportsFiltering)
{
bindingList.Filter = string.Format("{0}='{1}'", this.parentMember, parentValue);
}
IList innerList = this.cm.List;
int currentListIndex = 0;
PropertyDescriptorCollection properties = this.cm.GetItemProperties();
PropertyDescriptor pdParentMember = properties[this.parentMember];
PropertyDescriptor pdChildMember = properties[this.childMember];
PropertyDescriptor pdTextMember = properties[this.textMember];
//TreeNodeCollection currNodes = this.Nodes;
while (currentListIndex < innerList.Count)
{
object currObject = innerList[currentListIndex];
TreeLeafNode myFirstNode = new TreeLeafNode();
myFirstNode.Text = Convert.ToString(pdChildMember.GetValue(currObject));
myFirstNode.Value = pdChildMember.GetValue(currObject);
myFirstNode.Item = currObject;
//this.BuildTree(myFirstNode.Nodes, pdChildMember.GetValue(currObject));
currNodes.Add(myFirstNode);
currentListIndex += 1;
}
for (int i = 0; i < currNodes.Count; i++)
{
TreeLeafNode node = currNodes[i] as TreeLeafNode;
this.BuildTree(node.Nodes, node.Value);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
internal class TreeLeafNode : TreeNode
{
private int position;
private object value;
private object item;
public object Item
{
get { return item; }
set { item = value; }
}
public object Value
{
get { return this.value; }
set { this.value = value; }
}
public int Position
{
get { return position; }
set { position = value; }
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
internal class TreeLeafNode : TreeNode
{
private int position;
private object value;
private object item;
public object Item
{
get { return item; }
set { item = value; }
}
public object Value
{
get { return this.value; }
set { this.value = value; }
}
public int Position
{
get { return position; }
set { position = value; }
}
}
}