c# TreeView

树形绑定数据

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();
        }

 


 

 

posted @ 2012-07-10 21:00  Dieaz5  阅读(261)  评论(0编辑  收藏  举报