方法一:.将所有节点加载到目录树上:
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.Data.SqlClient;
using System.Configuration;
public partial class TreeView将所有节点加载到目录树上 : System.Web.UI.Page
{
string sqlstr=ConfigurationManager.ConnectionStrings["sqlstr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
GetData();
if (!IsPostBack)
{
LoadTreeview(this.TreeView1,null,0);
}
}
DataTable dt;
private void GetData()
{
using (SqlConnection sqlcnn=new SqlConnection(sqlstr))
{
using (SqlCommand sqlcmm=sqlcnn.CreateCommand())
{
sqlcmm.CommandText = "select * from T_Xzqh";
sqlcnn.Open();
dt = new DataTable();
SqlDataAdapter adapt = new SqlDataAdapter(sqlcmm);
adapt.Fill(dt);
sqlcmm.Dispose();
sqlcnn.Dispose();
}
}
}
private void LoadTreeview(TreeView tv, TreeNode tn, int ParentBh)
{
DataRow[] rows = dt.Select("ParentBh=" + ParentBh);
foreach (DataRow row in rows)
{
TreeNode node = new TreeNode();
node.Text = Convert.ToString(row["XzName"]);
node.Value = Convert.ToString(row["Xzbh"]);
if (tn == null)
{
tv.Nodes.Add(node);
}
else
{
tn.ChildNodes.Add(node);
}
LoadTreeview(this.TreeView1, node, Convert.ToInt32(row["Xzbh"]));
}
}
}
方法二:动态填充节点:
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.Data.SqlClient;
using System.Configuration;
public partial class TreeView动态填充节点 : System.Web.UI.Page
{
string sqlstr=ConfigurationManager.ConnectionStrings["sqlstr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{ GetData();
if (!IsPostBack)
{
LoadTreeView();
}
}
DataTable dt;
private void GetData()
{
using (SqlConnection sqlcnn=new SqlConnection(sqlstr))
{
using (SqlCommand sqlcmm=sqlcnn.CreateCommand())
{
sqlcmm.CommandText = "select * from T_Xzqh";
SqlDataAdapter adapter = new SqlDataAdapter(sqlcmm);
dt = new DataTable();
adapter.Fill(dt);
sqlcmm.Dispose();
sqlcnn.Dispose();
}
}
}
//获取顶级节点
private void LoadTreeView()
{
DataRow[] rows = dt.Select("parentbh="+0);
foreach (DataRow row in rows)
{
TreeNode node = new TreeNode();
node.Text = Convert.ToString(row["XzName"]);
node.Value = Convert.ToString(row["Xzbh"]);
node.PopulateOnDemand = true;
this.TreeView1.Nodes.Add(node);
}
}
protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
DataRow[] rows = dt.Select("parentbh="+e.Node.Value);
foreach (DataRow row in rows)
{
TreeNode node = new TreeNode();
node.Text = Convert.ToString(row["xzname"]);
node.Value = Convert.ToString(row["Xzbh"]);
//判断此节点下面是否有子节点,如果有就在前面显示加号(也就是设置此节点为动态填充),否则不予操作
DataRow[] rows1 = dt.Select("ParentBh=" + Convert.ToString(row["Xzbh"]));
if (rows1.Length>0)
{
node.PopulateOnDemand = true;
}
e.Node.ChildNodes.Add(node);
}
}
}
方法三:动态从数据库中获取需要的数据,动态填充节点
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.Data.SqlClient;
using System.Configuration;
public partial class TreeView动态从数据库中获取需要的数据_动态填充节点 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = GetDataTable(0);
foreach (DataRow row in dt.Rows)
{
TreeNode node = new TreeNode();
node.Text = Convert.ToString(row["XzName"]);
node.Value = Convert.ToString(row["Xzbh"]);
node.PopulateOnDemand = true;
this.TreeView1.Nodes.Add(node);
}
}
}
string sqlstr=ConfigurationManager.ConnectionStrings["sqlstr"].ConnectionString;
private DataTable GetDataTable(int parentbh)
{
using (SqlConnection sqlcnn=new SqlConnection(sqlstr))
{
using (SqlCommand sqlcmm=sqlcnn.CreateCommand())
{
sqlcmm.CommandText = "select * from T_Xzqh where parentbh="+parentbh;
SqlDataAdapter adapter = new SqlDataAdapter(sqlcmm);
DataTable dt = new DataTable();
adapter.Fill(dt);
sqlcmm.Dispose();
sqlcnn.Dispose();
return dt;
}
}
}
protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
List list = new List();
DataTable dt = GetDataTable(Convert.ToInt32(e.Node.Value));
foreach (DataRow row in dt.Rows)
{
TreeNode node = new TreeNode();
node.Text = Convert.ToString(row["XzName"]);
node.Value = Convert.ToString(row["Xzbh"]);
DataTable dt1 = GetDataTable(Convert.ToInt32(row["Xzbh"]));
if (dt1.Rows.Count>0)
{
node.PopulateOnDemand = true;
}
e.Node.ChildNodes.Add(node);
}
}
}
点此下载源码和数据库
TreeView加载数据库数据的三种方法实现三级联动
最新推荐文章于 2024-04-05 00:30:00 发布