我想以这种形式显示 TreeView ...
private void DatabaseToTreeView_Load(object sender, EventArgs e)
{
{
String connectionString = @"String here";
SqlConnection connection = new SqlConnection(connectionString);
DataSet ds = new DataSet();
connection.Open();
SqlCommand comm = new SqlCommand("Select CourseName from Course", connection);
SqlDataReader reader = comm.ExecuteReader();
treeView1.Nodes.Clear();
//Parent NODE-1
TreeNode prntnode = new TreeNode();
prntnode.Text = "Courses";
prntnode.ImageIndex = 0;
prntnode.SelectedImageIndex = 0;
this.treeView1.Nodes.Add(prntnode);
while (reader.Read())
{
prntnode.Nodes.Add(reader["CourseName"].ToString());
}
SqlCommand comm1 = new SqlCommand("Select BranchName from Branch INNER JOIN Course on Branch.CourseCode=Course.CourseCode", connection);
//SqlDataReader reader1 = comm.ExecuteReader();
// treeView1.ChildNode.Clear();
TreeNode ChildNode = new TreeNode("BranchName");
// ChildNode.Tag = "BranchName";
//ChildNode.Text = "BranchName";
ChildNode.ImageIndex = 1;
ChildNode.SelectedImageIndex = 1;
//treeView1.SelectedNode.Nodes.Add(ChildNode);
prntnode.Nodes.Add(ChildNode);
while (reader.Read())
{
ChildNode.Nodes.Add(reader["BranchName"].ToString());
}
comm.Connection.Close();
}
}