/**/
/// <summary>
/// TreeView控件工具包
/// </summary>
public
static
class
TreeViewControlUtil
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <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
)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//有效判断
if (NodeData == null &&
NodeData.Length <= 0 &&
RootNode == null)
return;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//临时表
System.Data.DataTable dt
= new System.Data.DataTable();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
dt.Columns.Add(new System.Data.DataColumn("Key", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("Value", typeof(String)));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
System.Reflection.PropertyInfo piKey
= typeof(T).GetProperty(CodeProperty);
System.Reflection.PropertyInfo piValue
= typeof(T).GetProperty(TextProperty);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (T Data in NodeData)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Data.DataRow dr = dt.NewRow();
dr["Key"] = KeyObject.ToString().Trim();
dr["Value"] = ValueObject.ToString().Trim();
dt.Rows.Add(dr);
}
catch
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ }
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (System.Data.DataRow dr in dt.Select(System.String.Empty, "Key ASC"))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Windows.Forms.TreeNode[] tnL = null;
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tnL =
RootNode.Nodes.Find(dr["Key"].ToString().Substring(0, dr["Key"].ToString().Length - Length), true);
}
catch
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ }
if (tnL != null && tnL.Length > 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tnL[0].Nodes.Add(dr["Key"].ToString(), dr["Value"].ToString());
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
RootNode.Nodes.Add(dr["Key"].ToString(), dr["Value"].ToString());
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
RootNode.ExpandAll();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
转载于:https://www.cnblogs.com/cgzwwy/articles/1174060.html