数据库 create database TreeView go use Treeview create table node1 ( nd1id int primary key, nd1name varchar(32) ) create table node2 ( nd2id int primary key, nd1id int, nd2name varchar(32) ) create table node3 ( nd3id int primary key, nd2i
数据库
create database TreeView
go
use Treeview
create table node1
(
nd1id int primary key,
nd1name varchar(32)
)
create table node2
(
nd2id int primary key,
nd1id int,
nd2name varchar(32)
)
create table node3
(
nd3id int primary key,
nd2id int,
nd3name varchar(32)
)
insert node1 values(0,'node1_1')
insert node1 values(1,'node1_2')
insert node1 values(2,'node1_3')
insert node1 values(3,'node1_4')
insert node2 values(0,0,'node2_1')
insert node2 values(1,0,'node2_2')
insert node2 values(2,1,'node2_3')
insert node2 values(3,1,'node2_4')
insert node2 values(4,2,'node2_5')
insert node2 values(5,3,'node2_6')
insert node2 values(6,2,'node2_7')
insert node3 values(0,0,'node3_1')
insert node3 values(1,0,'node3_2')
insert node3 values(2,1,'node3_3')
insert node3 values(3,2,'node3_4')
insert node3 values(4,1,'node3_5')
insert node3 values(5,3,'node3_6')
insert node3 values(6,5,'node3_7')
insert node3 values(7,6,'node3_8')
insert node3 values(8,3,'node3_9')
insert node3 values(9,4,'node3_10')
insert node3 values(10,5,'node3_11')
VS代码
void PopulateCategories(TreeNode node)
{
string sql = "Select nd1name, nd1id From node1";
DataSet resultSet;
resultSet = Getdataset(sql, null);
if (resultSet.Tables.Count > 0)
{
foreach (DataRow row in resultSet.Tables[0].Rows)
{
TreeNode NewNode = new TreeNode(row["nd1name"].ToString(), row["nd1id"].ToString());
NewNode.PopulateOnDemand = true;
NewNode.SelectAction = TreeNodeSelectAction.Expand;
node.ChildNodes.Add(NewNode);
}
}
}
void PopulateProducts(TreeNode node)
{
string sql = "Select nd2name,nd2id From node2 " + " Where nd1id = @id";
SqlParameter para = new SqlParameter("@id", SqlDbType.Int);
para.Value = node.Value;
//sqlQuery.Parameters.Add("@id", SqlDbType.Int).Value = node.Value;
DataSet ResultSet = Getdataset(sql, para);
if (ResultSet.Tables.Count > 0)
{
foreach (DataRow row in ResultSet.Tables[0].Rows)
{
TreeNode NewNode = new TreeNode(row["nd2name"].ToString(), row["nd2id"].ToString());
NewNode.PopulateOnDemand = true;
NewNode.SelectAction = TreeNodeSelectAction.Expand;
node.ChildNodes.Add(NewNode);
}
}
}
void PopulateProducts1(TreeNode node)
{
string sql = "Select nd3name,nd3id From node3 " + " Where nd2id = @id";
SqlParameter para = new SqlParameter("@id", SqlDbType.Int);
para.Value = node.Value;
//sqlQuery.Parameters.Add("@id", SqlDbType.Int).Value = node.Value;
DataSet ResultSet = Getdataset(sql, para);
if (ResultSet.Tables.Count > 0)
{
foreach (DataRow row in ResultSet.Tables[0].Rows)
{
TreeNode NewNode = new TreeNode(row["nd3name"].ToString(), row["nd3id"].ToString());
NewNode.PopulateOnDemand = false;
NewNode.SelectAction = TreeNodeSelectAction.SelectExpand;
node.ChildNodes.Add(NewNode);
}
}
}
private DataSet Getdataset(string sql,SqlParameter paralist)
{
string strconn = "database=treeview;server=.;Integrated Security=SSPI";
SqlConnection conn = new SqlConnection(strconn);
SqlCommand cmd = new SqlCommand();
if (paralist != null)
{
cmd.Parameters.Add(paralist);
}
cmd.CommandText = sql;
cmd.Connection = conn;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
if (e.Node.ChildNodes.Count == 0)
{
switch (e.Node.Depth)
{
case 0:
PopulateCategories(e.Node);
break;
case 1:
PopulateProducts(e.Node);
break;
case 2:
PopulateProducts1(e.Node);
break;
}
}
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
Response.Write("adfdf");
}
protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
{
//Response.Write("adfdf");
}