zoukankan      html  css  js  c++  java
  • winform中生成TreeView树

    无论是webform还是winform,TreeView都是常用功能。使用递归方法很方便。

    下面分享一个小实例。

    数据库中3个字段,分别是:ID,itemType_name,itemType_PID

    private void itemType_Load(object sender, EventArgs e)
            {
                dataBind();
            }
    
    
            private void dataBind()
            {
    
                //获取数据
                string sql = "select * from itemType order by itemType_paixu";
    
                DB db = new DB();
                string _msg = "";
                DataTable DT = db.GetDt(sql, out _msg);
                //
    
                treeView1.Nodes.Clear();
    
                //添加顶节点
                TreeNode tempNode = new TreeNode();
                tempNode.Text = "商品类别";
                tempNode.Name = "0";
                treeView1.Nodes.Add(tempNode);
                //
    
                nodeBind(treeView1, DT, "0", tempNode);
                treeView1.ExpandAll();
                //
            }
    
            public void nodeBind(TreeView _treeView, DataTable _dt, string _filter, TreeNode _pNode)
            {
    
                //循环构建树
                string sFilter = "itemType_PID=" + _filter;
                TreeNode parentNode = _pNode;
    
                DataView dv = new DataView(_dt);
    
                dv.RowFilter = sFilter;
                if (dv.Count > 0)
                {
                    foreach (DataRowView drv in dv)
                    {
                        TreeNode tempNode = new TreeNode();
    
                        tempNode.Text = drv["itemType_name"].ToString();
                        tempNode.Name = drv["ID"].ToString();
    
    
                        if (parentNode != null)
                            parentNode.Nodes.Add(tempNode);
                        else
                            _treeView.Nodes.Add(tempNode);
    
                        nodeBind(_treeView, _dt, drv["ID"].ToString(), tempNode);
                    }
                }
            }
    

      上面这个例子是有固定根节点的,下面这个例子是没有根节点的,递归构建更方便。

    private void codeitem_Load(object sender, EventArgs e)
            {
                dataBind();
            }
    
            private void dataBind()
            {
    
                //获取数据
                string sql = "select * from codeitem order by codeitem_paixu";
    
                DB db = new DB();
                string _msg = "";
                DataTable DT = db.GetDt(sql, out _msg);
                //
    
                treeView1.Nodes.Clear();
                nodeBind(treeView1, DT, "0", null);
                treeView1.ExpandAll();
                //
            }
    
            public void nodeBind(TreeView _treeView, DataTable _dt, string _filter, TreeNode _pNode)
            {
    
                //循环构建树
                string sFilter = "codeitem_pcode=’" + _filter + "’";
                TreeNode parentNode = _pNode;
    
                DataView dv = new DataView(_dt);
    
                dv.RowFilter = sFilter;
                if (dv.Count > 0)
                {
                    foreach (DataRowView drv in dv)
                    {
                        TreeNode tempNode = new TreeNode();
    
                        tempNode.Text = drv["codeitem_name"].ToString();
                        tempNode.Name = drv["ID"].ToString();
    
    
                        if (parentNode != null)
                            parentNode.Nodes.Add(tempNode);
                        else
                            _treeView.Nodes.Add(tempNode);
    
                        nodeBind(_treeView, _dt, drv["codeitem_code"].ToString(), tempNode);
                    }
                }
            }
    

      bubuko.com版权所有,禁止转载。原地址:【winform,TreeView树,树形菜单,bubufx】

  • 相关阅读:
    【LintCode题集】Q539
    【LintCode题解】Q407
    【LintCode题集】Q6、Q64
    【Java安全】关于Java中常用加密/解密方法的实现
    【MySQL】MySQL5.7的安装与配置
    理解CSS3 max/min-content及fit-content等width值
    Django和MySQL数据库第一次连接时遇到的若干问题及解决办法
    使用Pycharm社区版启动Django的重要补充
    使用Pycharm社区版新建Python3.7的虚拟环境并安装启动Django的完整步骤
    04-图形化编辑器功能不全?
  • 原文地址:https://www.cnblogs.com/weekzero/p/3483178.html
Copyright © 2011-2022 走看看