C# treeview on the binding and right-click menu to add delete child nodes

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Recently, the use of the WinForm treeview controls, look on the net a lot about treeview control using the methods described are not comprehensive. So I the rookie was writing something, please master don't laugh. And also as a reflection and record their own learning, the other friends if you don't mind can take to use. Without further ado, code:

 A, bind the treeview control the operation of the database

The database on the binding, reference online master who wrote the demo, don't laugh at me, ha ha.

Code


 1   public void AddTree(int ParentID, TreeNode pNode, TreeView productTypeTree)
 2 
 3               {         
 4 
 5                        string sql = "select category_sn, category_name, parent_sn from st_product_category";      
 6 
 7                           DataTable dt = DBHelper.ta(sql, 1);           
 8 
 9                         TreeNode tn1 = new TreeNode();         
10 
11                           DataView dvTree = new DataView(dt);       
12 
13                          //Filter ParentID, get all the child nodes of the current       
14 
15                       dvTree.RowFilter = "[parent_sn] = " + ParentID;           
16 
17                    foreach (DataRowView Row in dvTree)      
18 
19                   {           
20 
21                       if (pNode == null)        
22 
23                        {    //Add a root node
24 
25                           tn1.Text = Row["category_name"].ToString();      
26 
27                           productTypeTree.Nodes.Add(tn1);         
28 
29                           tn1.ExpandAll();                   
30 
31                           AddTree(Int32.Parse(Row["category_sn"].ToString()), tn1, productTypeTree);    //Once again the recursive    
32 
33                        }        
34 
35                     else    
36 
37                       {   //The child nodes of the current node is added     
38 
39                          TreeNode tn2 = new TreeNode();  
40 
41                          tn2.Text = Row["category_name"].ToString();      
42 
43                          pNode.Nodes.Add(tn2);            
44 
45                          tn1.ExpandAll();          
46 
47                          AddTree(Int32.Parse(Row["category_sn"].ToString()), tn2, productTypeTree);    //Once again the recursive
48 
49                         }          
50 
51                     }        
52 
53                    productTypeTree.ExpandAll();   
54 
55              }

Two,   in addition the right-click menu for the treeview control (add nodes, delete nodes)

Added right-click menu in the treeview node click event  (TreeView1_NodeMouseClick)

                     private void TreeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)

                    {        

                               if (e.Button != MouseButtons.Right) return;  

                               if (e.Node.Level == 0) return;        

                                TreeNode currentNode = this.productTypeTree.GetNodeAt(new Point(e.X, e.Y));    

                               ContextMenuStrip cms = new ContextMenuStrip();      

                               if (currentNode != null)   

                                   { 

                                        ToolStripMenuItem editAddproduct = new ToolStripMenuItem("Add sibling node");  

                                        ToolStripMenuItem editAddprChildoduct = new ToolStripMenuItem("Add a child node"); 

                                        ToolStripMenuItem editDeleteproduct = new ToolStripMenuItem("Delete node");      

                                        editAddproduct.Click += new EventHandler(editAddproduct_Click);         

                                        editAddprChildoduct.Click += new EventHandler(editAddChildProduct_Click);       

                                        editDeleteproduct.Click += new EventHandler(editDeleteproduct_Click);          

                                        cms.Items.Add(editAddproduct);    

                                        cms.Items.Add(editAddprChildoduct);   

                                        cms.Items.Add(editDeleteproduct);   

                                        cms.Show(this.productTypeTree, e.X, e.Y);   

                                        this.productTypeTree.SelectedNode = currentNode;   

                                    }

                      }

Three to achieve the right menu click event

 1             Add a child node
 2 
 3            void editAddChildProduct_Click(object sender, EventArgs e)
 4         {
 5             TreeNode tn = this.productTypeTree.SelectedNode;
 6             AddChildNode(tn, "The new node");
 7             this.productTypeTree.ExpandAll();
 8         }       
 9         //Delete node
10         void editDeleteproduct_Click(object sender, EventArgs e)
11         {
12             TreeNode treenode = this.productTypeTree.SelectedNode;
13             this.DeleteNode(treenode);
14             this.productTypeTree.ExpandAll();
15         }
16         //  Add sibling node
17         void editAddproduct_Click(object sender, EventArgs e)
18         {           
19             TreeNode tn = this.productTypeTree.SelectedNode;
20             AddtreeNode(tn, "The new node");
21             this.productTypeTree.ExpandAll();
22         }       
23 
24          //Add sibling node
25         private void AddtreeNode(TreeNode Node, string NewNodeName)
26         {
27             TreeNode parentNode = Node.Parent;
28             TreeNode newNode = new TreeNode(); newNode.Text = NewNodeName;           
29             if (parentNode != null)
30             {
31                 if (!parentNode.Nodes.Contains(newNode))
32                 {
33                     parentNode.Nodes.Add(newNode);
34                 }
35                 else return;                
36             }
37         }
38         //Add a child node
39         private void AddChildNode(TreeNode tn, string NewChileName)
40         {
41             TreeNode newChildNode = new TreeNode();
42             newChildNode.Text = NewChileName;
43             tn.Nodes.Add(newChildNode);
44         }
45 
46          //Delete node
47         private void DeleteNode(TreeNode treenode)
48         {
49             if (treenode.Nodes.Count==0)   //If the node has no child nodes directly to delete
50             {
51                 this.productTypeTree.Nodes.Remove(treenode);
52             }
53             else if (treenode.Nodes.Count>0)//This node node prompted to delete all contents of all the nodes
54             {
55                 if ((MessageBox.Show("Delete all items of the project? ","Warning",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning))==DialogResult.OK)
56                 {
57                     this.productTypeTree.Nodes.Remove(treenode);
58                 }
59             }
60         }

Extra explanation I don't write, with everyone to understand. There is no good algorithm, also hope to master Zhidianmijin.


2013-06-11




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Oliver at November 18, 2013 - 5:06 PM