点滴积累【C#】---TreeView读取数据库

效果:

点滴积累【C#】---TreeView读取数据库

数据库:

点滴积累【C#】---TreeView读取数据库

思路:

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

代码:

  1 using System;

  2 using System.Collections.Generic;

  3 using System.Linq;

  4 using System.Web;

  5 using System.Web.UI;

  6 using System.Web.UI.WebControls;

  7 using System.Data;

  8 using System.Collections;

  9 using System.Configuration;

 10 using System.Data.SqlClient;

 11 

 12 

 13 

 14 namespace TreeView

 15 {

 16     public partial class TreeView : System.Web.UI.Page

 17     {

 18         string connection = ConfigurationManager.ConnectionStrings["SQLconnection"].ToString();

 19         protected void Page_Load(object sender, EventArgs e)

 20         {

 21             select();

 22             CreateTree();

 23         }

 24         DataSet ds = new DataSet();

 25         public DataSet DataS(string sql, string tablename)

 26         {

 27             SqlConnection con = new SqlConnection(connection);

 28             SqlDataAdapter sqlda = new SqlDataAdapter(sql, con);

 29             sqlda.Fill(ds, tablename);

 30             return ds;

 31         }

 32 

 33         //ParentMenu:父节点,

 34         //Grade:标识这是第几级节点,

 35         //Flag:标识是不是尾结点,1是 0 不是,

 36         //href:指向的链接

 37         public void select()

 38         {

 39             string sql = "select * from treeview";

 40             ds = DataS(sql, "test");

 41         }

 42 

 43         public void CreateTree()

 44         {

 45             try

 46             {

 47                 DataTable dt = ds.Tables["test"];

 48                 SortedList sl = new SortedList();

 49                 TreeNode tn = new TreeNode();

 50                 if (dt.Rows.Count <= 0)

 51                 {

 52                     tn.Text = "";

 53                     tn.Value = "";

 54                     tn.NavigateUrl = "";

 55                     tn.Target = "";

 56                     tn.Expanded = false;

 57                     tn.SelectAction = TreeNodeSelectAction.SelectExpand;

 58 

 59                 }

 60                 else

 61                 {

 62                     for (int i = 0; i < dt.Rows.Count; i++)

 63                     {

 64                         //创建父节点

 65                         if (dt.Rows[i]["Grade"].ToString() == "1")

 66                         {

 67                             TreeNode node = new TreeNode();

 68                             node.Text = dt.Rows[i]["MenuName"].ToString();

 69                             node.Value = dt.Rows[i]["MenuID"].ToString();

 70                             if (dt.Rows[i]["Flag"].ToString() == "1")

 71                             {

 72                                 node.NavigateUrl = dt.Rows[i]["href"].ToString();

 73                                 tn.Target = "_bank";

 74                             }

 75                             node.Expanded = false;

 76                             node.SelectAction = TreeNodeSelectAction.Expand;

 77                             sl.Add(node.Value, node);

 78                             tv_Method.Nodes.Add(node);

 79                         }

 80                         else

 81                         {

 82                             //查找父节点

 83                             TreeNode ParentNod = (TreeNode)sl.GetByIndex(sl.IndexOfKey(dt.Rows[i]["ParentMenu"].ToString()));

 84                             //创建子节点

 85                             TreeNode tnChild = new TreeNode();

 86                             tnChild.Text = dt.Rows[i]["MenuName"].ToString();

 87                             tnChild.Value = dt.Rows[i]["MenuID"].ToString();

 88                             if (dt.Rows[i]["Flag"].ToString() == "1")

 89                             {

 90                                 tnChild.NavigateUrl = dt.Rows[i]["href"].ToString();

 91                                 tnChild.Target = "_bank";

 92                             }

 93                             tnChild.Expanded = false;

 94                             tnChild.SelectAction = TreeNodeSelectAction.Expand;

 95                             sl.Add(tnChild.Value, tnChild);

 96                             ParentNod.ChildNodes.Add(tnChild);

 97                         }

 98                     }

 99                 }

100             }

101             catch (Exception ex)

102             {

103                 throw ex;

104             }

105         }

106     }

107 }

 

你可能感兴趣的:(treeview)