类型:转载 责任编辑:asp.net 日期:2007/05/23
热门软件下载:
哪位大虾有从数据库中调用数据并绑定到treeview控件上的例子,是用C#写的
网友回答:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data .SqlClient;
using Microsoft.Web.UI.WebControls;
using System.Configuration;
using NewWork;
namespace km_new.bulletin.metting
{
/// <summary>
/// select_user 的摘要说明。
/// </summary>
public class select_user: System.Web.UI.Page
{
protected System.Web.UI.WebControls.ImageButton ImageButton1;
protected Microsoft.Web.UI.WebControls.TreeView TreeView1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack )
{
createroot(); //创建树
adduser(); //树中加用户
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.ImageButton1.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void createroot() //创建根节点
{
SqlConnection myConnection;
myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectString"]);
SqlDataAdapter myCommand = new SqlDataAdapter("select dep_name,dep_id from sys_dep_master", myConnection);
DataSet ds = new DataSet(); //取得所有群组名
myCommand.Fill(ds, "t1"); //保存sys_dep_master的所有dep_name,dep_id
for(int i=0;i<=ds.Tables [0].Rows.Count -1;i++)
{
TreeNode t1=new TreeNode ();
t1.ImageUrl =@"../../image/tree/root.gif";
t1.ID=ds.Tables [0].Rows [i][1].ToString ();
t1.Text =ds.Tables [0].Rows [i][0].ToString (); //取得所有dep_name和id
TreeView1.Nodes.Add(t1); //所有群组名加到根目录
string id=t1.ID ;
InitTree(t1.Nodes,id); //调用InitTree函数 形成所有子树
}
}
private void InitTree(TreeNodeCollection Nds,string masterId)
{
string str;
SqlConnection myConnection;
myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectString"]);
str="select * from sys_dep_chi where master_dep_id="+""+masterId+"";
SqlDataAdapter myCommand = new SqlDataAdapter(str, myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "t1");
for(int i=0;i<=ds.Tables ["t1"].Rows.Count-1;i++)
{
TreeNode tmpNd=new TreeNode();
tmpNd.ID =ds.Tables ["t1"].Rows[i][0].ToString ();
tmpNd.Text =ds.Tables ["t1"].Rows[i][1].ToString ();
tmpNd.ImageUrl =@"../../image/tree/root1.gif";
Nds.Add (tmpNd);
InitTree(tmpNd.Nodes,tmpNd.ID); //递归调用
}
}
private void adduser() //添加用户
{
GetAllNode(TreeView1.Nodes);
}
void GetAllNode(TreeNodeCollection tnc) //遍历树的每个节点
{
foreach(TreeNode node in tnc)
{
if(node.Nodes.Count!=0)
GetAllNode(node.Nodes);
adduser1(node.Nodes ,node.ID ); //每个节点的nodes集合和id作为参数,构造树
}
}
private void adduser1(TreeNodeCollection tnc,string id) //每个节点添加用户
{
SqlConnection myConnection;
myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectString"]);
SqlDataAdapter myCommand = new SqlDataAdapter("select user_name,a.user_id,dep_id from sys_user as a,sys_user_dep as b where a.user_id=b.user_id", myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "t1");
for(int i=0;i<=ds.Tables ["t1"].Rows.Count -1;i++)
if(id ==ds.Tables ["t1"].Rows[i][2].ToString ())
{
TreeNode t1=new TreeNode ();
t1.ImageUrl =@"../../image/tree/person.gif";
t1.ID =ds.Tables ["t1"].Rows[i][1].ToString ();
t1.Text =ds.Tables [0].Rows [i][0].ToString ();
tnc.Add (t1);
}
}
http://www.yesky.com/20030402/1660722.shtml
去搜索一下吧,很多