级别码生成树的代码
/// <summary>
/// TreeView控件工具包
/// </summary>
public static class TreeViewControlUtil
{
/// <summary>
/// 构建一颗级别树的方法
/// <example>
/// 调用方法:
/// <code>
/// TreeControlUtil.BuildTree<TestNode>(treeView1.Nodes[0],
/// new TestNode[] {
/// new TestNode("003","TomInfo"),
/// new TestNode("003001","Name"),
/// new TestNode("003002","Age"),
/// new TestNode("003003","Sex"),
/// new TestNode("004","JaneInfo"),
/// },
/// "Key", "Value", 3);
/// </code>
/// <code>
/// public class TestNode
/// {
/// public TestNode(System.String Key, System.String Value)
/// {
/// this.Key = Key;
/// this.Value = Value;
/// }
///
/// System.String key = System.String.Empty;
/// public System.String Key
/// {
/// get { return key; }
/// set { key = value.Trim(); }
/// }
/// System.String value = System.String.Empty;
/// public System.String Value
/// {
/// get { return value; }
/// set { this.value = value.Trim(); }
/// }
/// }
/// </code>
/// </example>
/// </summary>
/// <typeparam name="T">来源数据的类型</typeparam>
/// <param name="RootNode">根节点</param>
/// <param name="NodeData">来源数据数组</param>
/// <param name="CodeProperty">数据在树上的标识属性</param>
/// <param name="TextProperty">数据在树上的文字属性</param>
/// <param name="Length">标识级别长度</param>
public static void BuildTree<T>(
System.Windows.Forms.TreeNode RootNode,
T[] NodeData,
System.String CodeProperty,
System.String TextProperty,
System.Int32 Length
)
{
//有效判断
if (NodeData == null &&
NodeData.Length <= 0 &&
RootNode == null)
return;
//临时表
System.Data.DataTable dt
= new System.Data.DataTable();
dt.Columns.Add(new System.Data.DataColumn("Key", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("Value", typeof(String)));
System.Reflection.PropertyInfo piKey
= typeof(T).GetProperty(CodeProperty);
System.Reflection.PropertyInfo piValue
= typeof(T).GetProperty(TextProperty);
foreach (T Data in NodeData)
{
System.Object KeyObject = piKey.GetValue(Data, null);
System.Object ValueObject = piValue.GetValue(Data, null);
if (KeyObject != null &&
ValueObject != null &&
KeyObject.ToString().Trim().Length > 0 &&
ValueObject.ToString().Trim().Length > 0)
{
try
{
System.Data.DataRow dr = dt.NewRow();
dr["Key"] = KeyObject.ToString().Trim();
dr["Value"] = ValueObject.ToString().Trim();
dt.Rows.Add(dr);
}
catch
{ }
}
}
foreach (System.Data.DataRow dr in dt.Select(System.String.Empty, "Key ASC"))
{
System.Windows.Forms.TreeNode[] tnL = null;
try
{
tnL =
RootNode.Nodes.Find(dr["Key"].ToString().Substring(0, dr["Key"].ToString().Length - Length), true);
}
catch
{ }
if (tnL != null && tnL.Length > 0)
{
tnL[0].Nodes.Add(dr["Key"].ToString(), dr["Value"].ToString());
}
else
{
RootNode.Nodes.Add(dr["Key"].ToString(), dr["Value"].ToString());
}
}
RootNode.ExpandAll();
}
}
/// TreeView控件工具包
/// </summary>
public static class TreeViewControlUtil
{
/// <summary>
/// 构建一颗级别树的方法
/// <example>
/// 调用方法:
/// <code>
/// TreeControlUtil.BuildTree<TestNode>(treeView1.Nodes[0],
/// new TestNode[] {
/// new TestNode("003","TomInfo"),
/// new TestNode("003001","Name"),
/// new TestNode("003002","Age"),
/// new TestNode("003003","Sex"),
/// new TestNode("004","JaneInfo"),
/// },
/// "Key", "Value", 3);
/// </code>
/// <code>
/// public class TestNode
/// {
/// public TestNode(System.String Key, System.String Value)
/// {
/// this.Key = Key;
/// this.Value = Value;
/// }
///
/// System.String key = System.String.Empty;
/// public System.String Key
/// {
/// get { return key; }
/// set { key = value.Trim(); }
/// }
/// System.String value = System.String.Empty;
/// public System.String Value
/// {
/// get { return value; }
/// set { this.value = value.Trim(); }
/// }
/// }
/// </code>
/// </example>
/// </summary>
/// <typeparam name="T">来源数据的类型</typeparam>
/// <param name="RootNode">根节点</param>
/// <param name="NodeData">来源数据数组</param>
/// <param name="CodeProperty">数据在树上的标识属性</param>
/// <param name="TextProperty">数据在树上的文字属性</param>
/// <param name="Length">标识级别长度</param>
public static void BuildTree<T>(
System.Windows.Forms.TreeNode RootNode,
T[] NodeData,
System.String CodeProperty,
System.String TextProperty,
System.Int32 Length
)
{
//有效判断
if (NodeData == null &&
NodeData.Length <= 0 &&
RootNode == null)
return;
//临时表
System.Data.DataTable dt
= new System.Data.DataTable();
dt.Columns.Add(new System.Data.DataColumn("Key", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("Value", typeof(String)));
System.Reflection.PropertyInfo piKey
= typeof(T).GetProperty(CodeProperty);
System.Reflection.PropertyInfo piValue
= typeof(T).GetProperty(TextProperty);
foreach (T Data in NodeData)
{
System.Object KeyObject = piKey.GetValue(Data, null);
System.Object ValueObject = piValue.GetValue(Data, null);
if (KeyObject != null &&
ValueObject != null &&
KeyObject.ToString().Trim().Length > 0 &&
ValueObject.ToString().Trim().Length > 0)
{
try
{
System.Data.DataRow dr = dt.NewRow();
dr["Key"] = KeyObject.ToString().Trim();
dr["Value"] = ValueObject.ToString().Trim();
dt.Rows.Add(dr);
}
catch
{ }
}
}
foreach (System.Data.DataRow dr in dt.Select(System.String.Empty, "Key ASC"))
{
System.Windows.Forms.TreeNode[] tnL = null;
try
{
tnL =
RootNode.Nodes.Find(dr["Key"].ToString().Substring(0, dr["Key"].ToString().Length - Length), true);
}
catch
{ }
if (tnL != null && tnL.Length > 0)
{
tnL[0].Nodes.Add(dr["Key"].ToString(), dr["Value"].ToString());
}
else
{
RootNode.Nodes.Add(dr["Key"].ToString(), dr["Value"].ToString());
}
}
RootNode.ExpandAll();
}
}