樹形綁定資料
SQL語句:提出需要綁定的父節點和子節點的資料語句,一般包括要顯示的資料和主鍵ID。
如:
(父節點)
if @type='dep'
begin
select departmentName, departmentId from wb_department
end
(子節點)
if @type='pro'
begin
select professionId,professionName,departmentId from wb_profession
end
c#代碼
public DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
ds.Tables.Add(Class1.dep());
ds.Tables[0].TableName = "depTable";
ds.Tables.Add(Class1.pro());
ds.Tables[1].TableName = "proTable";
ds.Relations.Add("dep_pro", ds.Tables[0].Columns["departmentId"], ds.Tables[1].Columns["departmentId"]);
foreach (DataRow FatherRow in ds.Tables[0].Rows)
{
TreeNode FatherNode = new TreeNode((string)FatherRow["departmentName"]);
//asp.net中c#語句是這樣: FatherNode.Target = FatherRow["departmentId"].ToString();
FatherNode.Tag = FatherRow["departmentId"].ToString();
TreeView1.Nodes.Add(FatherNode);
foreach (DataRow ChildrenRow in FatherRow.GetChildRows("dep_pro"))
{
TreeNode ChildrenNode = new TreeNode((string)ChildrenRow["professionName"]);
//ChildrenNode.Target = ChildrenRow["professionId"].ToString();
ChildrenNode.Tag= ChildrenRow["professionId"].ToString();
//FatherNode.ChildNodes.Add(ChildrenNode);
FatherNode.Nodes.Add(ChildrenNode);
}
}
}
動態添加父節點
TreeNode node = new TreeNode();
node.Text = textBox1.Text.ToString();
this.treeView1.Nodes.Add(node);
TreeNode node1 = new TreeNode();
this.treeView1.Nodes[0].Nodes.Add(node1);
TreeView-----節點單擊事件:
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
Form form2 = new Form();
form2.ShowDialog();
}