首页 > 布布扣 > 详细

winform中生成TreeView树

时间:2014-12-02 16:51:37      阅读:1110      评论:0      收藏: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);
                }
            }
        }



winform中生成TreeView树

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!