• TreeView相关类


    #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

  • 相关阅读:
    Atitit. C#.net clr 2.0 4.0 4.5新特性 v2 s22 1. CLR内部结构 1 2. CLR 版本发展史 3 3. CLR 2.0新特性 4 4. CLR 4 新特性
    Hbase基本命令 悟寰轩
    mvn常用命令 悟寰轩
    linux添加tomcat服务 悟寰轩
    hadoop基本命令 悟寰轩
    Tomcat启动 悟寰轩
    Eclipse自动部署项目到Tomcat的webapps下的有效方法 悟寰轩
    MySQL改变默认编码为utf8 悟寰轩
    myeclipse关闭自动更新 悟寰轩
    Linux命令大全 悟寰轩
  • 原文地址:https://www.cnblogs.com/zlf344242525/p/2254885.html
Copyright © 2020-2023  润新知