c# TreeView

來源:互聯網
上載者:User

樹形綁定資料

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

 


 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.