VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • C#教程之C# TreeView读取数据库简单实例

效果:

数据库:

思路:

利用for遍历,然后创建父节点,再根据父节点创建出子节点。

代码:

复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using System.Configuration;
using System.Data.SqlClient;

 

 

namespace TreeView
{
    public partial class TreeView : System.Web.UI.Page
    {
        string connection = ConfigurationManager.ConnectionStrings["SQLconnection"].ToString();
        protected void Page_Load(object sender, EventArgs e)
        {
            select();
            CreateTree();
        }
        DataSet ds = new DataSet();
        public DataSet DataS(string sql, string tablename)
        {
            SqlConnection con = new SqlConnection(connection);
            SqlDataAdapter sqlda = new SqlDataAdapter(sql, con);
            sqlda.Fill(ds, tablename);
            return ds;
        }

        //ParentMenu:父节点,
        //Grade:标识这是第几级节点,
        //Flag:标识是不是尾结点,1是 0 不是,
        //href:指向的链接
        public void select()
        {
            string sql = "select * from treeview";
            ds = DataS(sql, "test");
        }

        public void CreateTree()
        {
            try
            {
                DataTable dt = ds.Tables["test"];
                SortedList sl = new SortedList();
                TreeNode tn = new TreeNode();
                if (dt.Rows.Count <= 0)
                {
                    tn.Text = "空";
                    tn.Value = "";
                    tn.NavigateUrl = "";
                    tn.Target = "";
                    tn.Expanded = false;
                    tn.SelectAction = TreeNodeSelectAction.SelectExpand;

                }
                else
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        //创建父节点
                        if (dt.Rows[i]["Grade"].ToString() == "1")
                        {
                            TreeNode node = new TreeNode();
                            node.Text = dt.Rows[i]["MenuName"].ToString();
                            node.Value = dt.Rows[i]["MenuID"].ToString();
                            if (dt.Rows[i]["Flag"].ToString() == "1")
                            {
                                node.NavigateUrl = dt.Rows[i]["href"].ToString();
                                tn.Target = "_bank";
                            }
                            node.Expanded = false;
                            node.SelectAction = TreeNodeSelectAction.Expand;
                            sl.Add(node.Value, node);
                            tv_Method.Nodes.Add(node);
                        }
                        else
                        {
                            //查找父节点
                            TreeNode ParentNod = (TreeNode)sl.GetByIndex(sl.IndexOfKey(dt.Rows[i]["ParentMenu"].ToString()));
                            //创建子节点
                            TreeNode tnChild = new TreeNode();
                            tnChild.Text = dt.Rows[i]["MenuName"].ToString();
                            tnChild.Value = dt.Rows[i]["MenuID"].ToString();
                            if (dt.Rows[i]["Flag"].ToString() == "1")
                            {
                                tnChild.NavigateUrl = dt.Rows[i]["href"].ToString();
                                tnChild.Target = "_bank";
                            }
                            tnChild.Expanded = false;
                            tnChild.SelectAction = TreeNodeSelectAction.Expand;
                            sl.Add(tnChild.Value, tnChild);
                            ParentNod.ChildNodes.Add(tnChild);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}



相关教程