欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

TreeView控件的绑定

程序员文章站 2022-06-08 10:19:18
...
 

关于TreeView控件的绑定,在网上查了很多资料,练习了在两种情况下的绑定,一种是在网页后台直接绑定,一种是在三层架构中绑定。 

数据库表的设计

商品小类表(tb_type)

字段名

数据类型

主键

外键

非空

含义

type_id

int

小类编号

type_name

nvarchar(50)

小类名称

category_id

int

大类编号

intro

text

介绍

 商品大类表(tb_ category)

字段名

数据类型

主键

外键

非空

含义

category_id

int

大类编号

category_name

varchar(255)

大类名称

intro

text

介绍

 

网页后台直接绑定:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dtFather=exe_Table("server=localhost;user id=root;password=root;database=em", "select distinct tb_category.category_id,tb_category.category_name FROM   tb_category");
            foreach(DataRow dr in dtFather.Rows)
            {
                
                TreeNode tn = new TreeNode();
                tn.Value = dr["category_id"].ToString();
                tn.Text = dr["category_name"].ToString();
                TreeView1.Nodes.Add(tn);
                DataTable dtChild = exe_Table("server=localhost;user id=root;password=root;database=em",
                    "select type_id,type_name,category_id from tb_type where tb_type.category_id=" + dr["category_id"].ToString());
                foreach (DataRow drChild in dtChild.Rows)
                {
                    TreeNode tnChild = new TreeNode();
                    tnChild.Value = drChild["type_id"].ToString();//节点的Value值,一般为数据库的id值
                    tnChild.Text = drChild["type_name"].ToString();//节点的Text,节点的文本显示
                    tn.ChildNodes.Add(tnChild);
                }

            }
        }
    }



          /// <summary>
        /// 取出数据库中数据,生成DataTable
        /// </summary>
        /// <param name="str_Con">数据库连接</param>
        /// <param name="str_Cmd">sql语句</param>
        /// <returns></returns>
        private DataTable exe_Table(string str_Con,string str_Cmd)
        {
            DataSet ds = new DataSet();
            using (MySqlConnection conn = new MySqlConnection(str_Con))
            {
                using (MySqlDataAdapter oda = new MySqlDataAdapter(str_Cmd, conn))
                {
                    conn.Open();
                    oda.Fill(ds);
                }
            }
            return ds.Tables[0];
        }


}

 

 

三层架构绑定:

Model实体类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Model
{
    public class PNode
    {
        public int id { get; set; }
        public string name { get; set; }
        public string intro { get; set; }
    }
}

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Model
{
    public class Node
    {
        public int id { get; set; }
        public string name { get; set; }
        public int pid { get; set; }
        public string intro { get; set; }
    }
}

 

DAL:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;
using Model;
namespace DAL
{
    public class ListTreeNode
    {
        public static List<PNode> GetPNode()
        {
            string strSql = "select distinct category_id,category_name ,intro from tb_category";
            List<PNode> list = new List<PNode>();
            using (MySqlConnection cn = new MySqlConnection(StrCon.Conn))
            {
                cn.Open();
                MySqlDataAdapter sda = new MySqlDataAdapter(strSql, cn);
                DataSet ds = new DataSet();
                sda.Fill(ds);
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    PNode pnode = new PNode();
                    pnode.id = (int)dr["category_id"];
                    pnode.name = dr["category_name"].ToString();
                    pnode.intro = dr["intro"].ToString();
                    list.Add(pnode);
                }
                return list;
            }
        }

        public static List<Node> GetNode(int pnode_id)
        {
            string strSql ="select type_id,type_name,tb_type.intro,category_id from tb_type where tb_type.category_id=" + pnode_id;
            List<Node> list = new List<Node>();
            using (MySqlConnection cn = new MySqlConnection(StrCon.Conn))
            {
                cn.Open();
                MySqlDataAdapter sda = new MySqlDataAdapter(strSql, cn);
                DataSet ds = new DataSet();
                sda.Fill(ds);
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    Node node = new Node();
                    node.id = (int)dr["type_id"];
                    node.name = dr["type_name"].ToString();
                    node.intro = dr["intro"].ToString();
                    list.Add(node);
                }
                return list;
            }
        }
    }
}

 

BLL:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using DAL;
using Model;

namespace BLL
{
    public class VarityManage
    {
        //获得根结点信息
        public static List<PNode> GetPNode()
        {
            return DAL.ListTreeNode.GetPNode(); 

        }
        //获得叶结点信息
        public static List<Node> GetNode(int pnode_id)
        {
            return DAL.ListTreeNode.GetNode(pnode_id);
        }

    }
}

 

UI:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;

namespace E_mall_background_system.Management
{
    public partial class main : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTreeView();
                BindddlSelectPNode1();
            }
        }

        //TreeView的绑定
        public void BindTreeView()
        {
            TreeView1.Nodes.Clear();
            int rootId = 0;
            string rootName = "所有商品种类";
            TreeNode root = CreateTreeNode(rootId, rootName);
            TreeView1.Nodes.Add(root);
            List<PNode> pnodes = VarityManage.GetPNode();
            foreach (PNode pnode in pnodes)
            {
                int nodeId = (int)pnode.id;
                string nodeName = pnode.name.ToString();
                TreeNode treeNode = this.CreateTreeNode(nodeId, nodeName);
                root.ChildNodes.Add(treeNode);
                List<Node> nodes = VarityManage.GetNode(nodeId);
                foreach (Node node in nodes)
                {
                    int childNodeId =(int) node.id;
                    string childNodeName = node.name.ToString();
                    TreeNode childTreeNode = this.CreateTreeNode(childNodeId, childNodeName);
                    treeNode.ChildNodes.Add(childTreeNode);
                }
                
            }
        }

        public TreeNode CreateTreeNode(int id, string name)
        {
            TreeNode tn = new TreeNode();
            tn.Value = id.ToString();
            tn.Text = name;
            return tn;
        }

    }
}

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/Miko2012/archive/2012/04/30/2474558.html