#region TreeView相关类
/// <summary>
/// 根据DATATABLE加载目录树
/// </summary>
/// <param name="parentID">最顶级节点父节点的值</param>
/// <param name="pNode">一般为NULL</param>
/// <param name="dt">存放子父节点的数据表</param>
/// <param name="tv">控件树</param>
/// <param name="Pid">数据表中父节点ID列名</param>
/// <param name="Id">数据表中节点ID列名</param>
/// <param name="NodeText">数据表中节点显示名列名</param>
/// <param name="NodeTag">数据表中节点Tag列名</param>
public static void Load_treeView(string parentID, TreeNode pNode,DataTable dt,TreeView tv,string Pid,string Id,string NodeText,string NodeTag)
{
try
{
DataView dvTree = new DataView(dt);
dvTree.RowFilter = "" + Pid + "='" + parentID + "'";
foreach (DataRowView Row in dvTree)
{
TreeNode Node = new TreeNode("", 0, 0);
Node.ImageIndex = 0;
Node.SelectedImageIndex = 1;
Node.Name = Row[Id].ToString();
Node.Text = Row[NodeText].ToString().Trim();
if (pNode == null)
{
if (NodeTag == "" || NodeTag == null)
{
}
else
{
Node.Tag = Row[NodeTag].ToString().Trim();
}
tv.Nodes.Add(Node);
}
else
{
if (NodeTag == "" || NodeTag == null)
{
}
else
{
Node.Tag = Row[NodeTag].ToString().Trim();
}
pNode.Nodes.Add(Node);
}
Load_treeView(Node.Name.ToString(), Node, dt, tv, Pid, Id, NodeText, NodeTag);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
public static void Load_treeView_WithCheckBox(string parentID, TreeNode pNode, DataTable dt, TreeView tv, string Pid, string Id, string NodeText, string NodeTag)
{
try
{
DataView dvTree = new DataView(dt);
dvTree.RowFilter = "" + Pid + "='" + parentID + "'";
foreach (DataRowView Row in dvTree)
{
TreeNode Node = new TreeNode("", 0, 0);
Node.ImageIndex = 0;
Node.SelectedImageIndex = 1;
if (pNode == null)
{
Node.Name = Row[Id].ToString();
Node.Text = Row[NodeText].ToString().Trim();
if (NodeTag == "" || NodeTag == null)
{
}
else if (Row[NodeTag].ToString().Trim() == "1")
{
Node.Checked = true;
Node.Tag = 1;
}
else if (Row[NodeTag].ToString().Trim() == "2")
{
Node.Checked = true;
Node.ForeColor = System.Drawing.Color.Red;
Node.Tag = 2;
}
tv.Nodes.Add(Node);
}
else
{
Node.Name = Row[Id].ToString();
Node.Text = Row[NodeText].ToString().Trim();
if (NodeTag == "" || NodeTag == null)
{
}
else if (Row[NodeTag].ToString().Trim() == "1")
{
Node.Checked = true;
Node.Tag = 1;
}
else if (Row[NodeTag].ToString().Trim() == "2")
{
Node.Checked = true;
Node.ForeColor = System.Drawing.Color.Red;
Node.Tag = 2;
}
pNode.Nodes.Add(Node);
}
Load_treeView_WithCheckBox(Node.Name.ToString(), Node, dt, tv, Pid, Id, NodeText, NodeTag);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
/// <summary>
/// 遍历目录树,定位到指定的节点上去并选中
/// </summary>
/// <param name="tv">所在树</param>
/// <param name="NodeFlagType">比较类型:1表示比较Tag,2表示比较Name,3表示比较Text</param>
/// <param name="CompString">比较的字符串</param>
public static void FindTreeNode(TreeView tv, int NodeFlagType, string CompString)
{
foreach (TreeNode tn in tv.Nodes)
{
FindNode(tn,tv,NodeFlagType,CompString);
}
}
/// <summary>
/// 遍历目录树,定位到指定的节点上去并选中
/// </summary>
/// <param name="node">需查找的节点</param>
/// <param name="tv">所在树</param>
/// <param name="NodeFlagType">比较类型:1表示比较Tag,2表示比较Name,3表示比较Text</param>
/// <param name="CompString">比较的字符串</param>
private static void FindNode(TreeNode node, TreeView tv, int NodeFlagType, string CompString)
{
try
{
switch (NodeFlagType)
{
case 1:
if (node.Tag.ToString() == CompString)
{
node.Expand();
tv.SelectedNode = node;
return;
}
break;
case 2:
if (node.Name.ToString() == CompString)
{
node.Expand();
tv.SelectedNode = node;
return;
}
break;
case 3:
if (node.Text.ToString() == CompString)
{
node.Expand();
tv.SelectedNode = node;
return;
}
break;
}
if (node.Nodes.Count > 0)
{
foreach (TreeNode n in node.Nodes)
{
FindNode(n, tv, NodeFlagType, CompString);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
#endregion