星期零

技术改变生活,分享让我们快乐!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

winform中生成TreeView树

Posted on 2013-12-20 08:36  weekzero  阅读(3054)  评论(0编辑  收藏  举报

无论是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】