使用TREEVIEW控件编写的一段代码,想了N久才出来
protected void TreeView1_Load()
{
//用于填充TreeView1
SqlCommand Mycommand1 = new SqlCommand("select * from CHAPTER where Course_ID=0", Myconn);
SqlDataReader DR1 = Mycommand1.ExecuteReader();
//SqlDataAdapter DA1 = new SqlDataAdapter(Mycommand1);
//DataSet DS1 = new DataSet("temp");
//DA1.Fill(DS1);
SqlCommand Mycommand2;
SqlDataReader DR2;
while(DR1.Read())
{
TreeNode NewNode = new TreeNode(DR1["Chapter_Name"].ToString());
NewNode.Value = DR1["Chapter_ID"].ToString();
NewNode.Collapse();
//二层结点--->
//Mycommand2 = new SqlCommand("select * from SECTION where Chapter_ID="+DR1["Chapter_ID"].ToString(),Myconn);
//DR2 = Mycommand2.ExecuteReader();
//TreeNode NewNode2 = new TreeNode(DR2["Section_Name"].ToString());
//NewNode.ChildNodes.Add(NewNode2);
//<<-----
TreeView1.Nodes.Add(NewNode);
}
DR1.Close();
foreach (TreeNode NewNode in TreeView1.Nodes)
{
Mycommand2 = new SqlCommand("select * from SECTION where Chapter_ID=" + NewNode.Value.ToString(), Myconn);
DR2 = Mycommand2.ExecuteReader();
while (DR2.Read())
{
TreeNode TempNode = new TreeNode(DR2["Section_Name"].ToString());
TempNode.NavigateUrl = "#";
TempNode.Target = "_blank";
NewNode.ChildNodes.Add(TempNode);
}
DR2.Close();
}
/*
if (DS1.Tables["CHAPTER"].Rows.Count.CompareTo(0)>0)
{
foreach (DataRow Row in DS1.Tables["CHAPTER"].Rows)
{
TreeNode NewNode = new TreeNode(Row["Chapter_Name"].ToString());
//二层结点--->
Mycommand2 = new SqlCommand("select * from SECTION where Chapter_ID=" + Row["Chapter_Name"].ToString(), Myconn);
DR2 = Mycommand2.ExecuteReader();
TreeNode NewNode2 = new TreeNode(DR2["Section_Name"].ToString());
NewNode.ChildNodes.Add(NewNode2);
//<<---------
TreeView1.Nodes.Add(NewNode);
}
}
DS1.Dispose();
*/
}