下面是我工作中遇到读取树结构 (即如何将一树结构的数据绑定到一个TreeView,级数可扩展) 时找到一个小方法,作为收藏并共享给大家
。
1.数据表结构如下:
| FID | FNodeKe | FParentNodeKey | FFunctionNa |
| 66 | F0001 | F0000 | ROOT1 |
| 67 | F0002 | F0000 | ROOT2 |
| 68 | F0003 | F0000 | ROOT3 |
| 69 | F0004 | F0000 | ROOT4 |
| 70 | F0005 | F0000 | ROOT5 |
| 71 | F0006 | F0000 | ROOT6 |
| 72 | F0007 | F0006 | R6-1 |
| 73 | F0008 | F0006 | R6-2 |
| 74 | F0009 | F0006 | R6-3 |
| 75 | F0010 | F0006 | R6-4 |
| 76 | F0011 | F0006 | R6-5 |
| 77 | F0012 | F0006 | R6-6 |
| 78 | F0013 | F0006 | R6-7 |
| 79 | F0014 | F0006 | R6-8 |
| 80 | F0015 | F0006 | R6-9 |
| 81 | F0016 | F0006 | R6-10 |
| 83 | F0018 | F0017 | R17-1 |
| 84 | F0019 | F0017 | R17-2 |
| 85 | F0020 | F0017 | R17-3 |
| 86 | F0021 | F0017 | R17-4 |
| 87 | F0022 | F0017 | R17-5 |
| 88 | F0023 | F0017 | R17-6 |
| 89 | F0024 | F0017 | R17-7 |
| 90 | F0025 | F0017 | R17-8 |
| 91 | F0026 | F0017 | R17-9 |
| 92 | F0027 | F0017 | R17-10 |
| 93 | F0028 | F0017 | R17-1 |
| 94 | F0029 | F0017 | R17-12 |
| 82 | F0017 | F0000 | ROOT7 |
| 100 | F0029 | F0000 | ROOT8 |
| 101 | F0030 | F0000 | ROOT9 |
| 102 | F0031 | F0030 | R30-1 |
| 103 | F0032 | F0030 | R30-2 |
| 104 | F0033 | F0030 | R30-3 |
| 105 | F0034 | F0030 | R30-4 |
2、页面文件Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
http://www.w3.org/1999/xhtml">
3、代码文件Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.bindtvUserGroup();
}
#region 为TreeView添加节点
public void bindtvUserGroup()//为树添加一个 "总目录".
{
this.tvUGManage.Nodes.Clear();
TreeNode tnUG = new TreeNode();
tnUG.Text = "F0000根目录";
tnUG.Value = "F0000";
tnUG.Expanded = true;
tvUGManage.Nodes.Add(tnUG);
bindtvParentUserGroup(tnUG);
}
public void bindtvParentUserGroup(TreeNode tnTwo)//在总目录下...添加 "父级树结点"
{
string sqlString = "select FID,FNodeKey,FParentNodeKey,FFunctionName from B_Functions";
string CnnString = "Data Source=(local);Initial Catalog=Test;User ID=sa;Password=111111;";
SqlConnection myconn = new SqlConnection(CnnString);
myconn.Open();
SqlCommand B_ZSGC = new SqlCommand();
B_ZSGC.Connection = myconn;
B_ZSGC.CommandType = CommandType.Text;
B_ZSGC.CommandText = sqlString;
SqlDataAdapter dt = new SqlDataAdapter();
dt.SelectCommand = B_ZSGC;
DataSet dsBF = new DataSet();
dt.Fill(dsBF);
DataTable dtBF = dsBF.Tables[0];
//this.tvUserGroup.Nodes.Clear();
DataRow[] RowList = dtBF.Select("FParentNodeKey = 'F0000'");
if (RowList.Length > 0)
{
for (int i = 0; i < RowList.Length; i++)
{
TreeNode tnTwoUG = new TreeNode();
tnTwoUG.Text = RowList[i]["FFunctionName"].ToString();
tnTwoUG.Value = RowList[i]["FNodeKey"].ToString();
tnTwoUG.Expanded = false;
tnTwo.ChildNodes.Add(tnTwoUG);//总目录下添加子结点(父级树结点)
bindtvChildUserGroup(tnTwoUG, dtBF);
}
}
}
public void bindtvChildUserGroup(TreeNode tnThre, DataTable dtChild)//在父级树结点下添加 "子结点"
{
DataRow[] RowList = dtChild.Select("FParentNodeKey = '" + tnThre.Value + "'");
if (RowList.Length > 0)
{
for (int j = 0; j < RowList.Length; j++)
{
TreeNode tnThreUG = new TreeNode();
tnThreUG.Text = RowList[j]["FFunctionName"].ToString();
tnThreUG.Value = RowList[j]["FNodeKey"].ToString();
//tnThreUG.Expanded = false;
tnThre.ChildNodes.Add(tnThreUG);
bindtvChildUserGroup(tnThreUG, dtChild); //循环遍历子节点.
}
}
}
#endregion
}
4、完成效果忘了截图,自己试一下吧!
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/14368614/viewspace-327250/ ,如需转载,请注明出处,否则将追究法律责任。