级别树生成代码更新
/// <summary>
/// TreeView控件工具包
/// </summary>
public static class TreeViewControlUtil
{
构建一颗级别树的方法
/// </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 = null;
System.Reflection.PropertyInfo piValue = null;
if (!(NodeData is System.Data.DataRow))
{
piKey = typeof(T).GetProperty(CodeProperty);
piValue = typeof(T).GetProperty(TextProperty);
}
foreach (T Data in NodeData)
{
if (piKey == null && piValue == null)
{
try
{
System.Data.DataRow dr2 = Data as System.Data.DataRow;
System.Data.DataRow dr = dt.NewRow();
dr["Key"] = dr2[CodeProperty].ToString();
dr["Value"] = dr2[TextProperty].ToString();
dt.Rows.Add(dr);
}
catch
{ }
}
else
{
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
{
构建一颗级别树的方法
/// </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 = null;
System.Reflection.PropertyInfo piValue = null;
if (!(NodeData is System.Data.DataRow))
{
piKey = typeof(T).GetProperty(CodeProperty);
piValue = typeof(T).GetProperty(TextProperty);
}
foreach (T Data in NodeData)
{
if (piKey == null && piValue == null)
{
try
{
System.Data.DataRow dr2 = Data as System.Data.DataRow;
System.Data.DataRow dr = dt.NewRow();
dr["Key"] = dr2[CodeProperty].ToString();
dr["Value"] = dr2[TextProperty].ToString();
dt.Rows.Add(dr);
}
catch
{ }
}
else
{
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();
}
}