Home > DeveloperSection > Articles > How to use Tree View Control in C#

How to use Tree View Control in C#


C# C# 
Ratings:
2 Comment(s)
 13282  View(s)
Rate this:

How to use Tree View Control in C#

Windows forms Tree View control helps to represent data in hierarchical format. Whenever you want to display data in the format of parent child relationship or in hierarchical format then we can use concept of tree view control. The best example of tree view control is to show hierarchy of company, windows file system etc.

In next few paragraph am demonstrating you the use of tree view control. I had made a small program which explains certain functionality of tree view controls such as how to add parent node dynamically, how to add child node dynamically, how to delete any selected node.

Example to demonstrate use of Tree View Control

How to use Tree View Control in C#

In this example I had taken two panel control, one tree view control, one text box control and three button control. I had added tree view control at first panel and add group of textbox and buttons in second panel. Add Parent Node button is used to add Parent Node and Add Child Node Button is used to add child Node to a specific parent node while Delete node button is used to delete selected node. In textbox we enter the name of the node which is further added as a child or parent node.

Adding Parent Node in Tree View Control

For adding parent node enter the name of the node in a textbox and click Add Parent Node button.

How to use Tree View Control in C#

Code to Add Parent Node

private void button1_Click(object sender, EventArgs e)

{

            if (textBox1.Text.Trim().Length != 0) //if textbox is not empty.

            {

                TreeNode parentNode = new TreeNode(textBox1.Text); //create an object of //Tree Node class and pass node name to the constructor of Tree Node.

                treeView1.Nodes.Add(parentNode); //Adding parent node to a tree view.

                textBox1.Clear();//Clear the text box control.

            }

            else

            {//If textbox is empty the message box appears to show a message to enter //value in textbox control.

                MessageBox.Show("Please Enter Value In The TextBox.", "Data Entry Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);

            }

}

Adding Child Node in Tree View Control

For adding any child node in a tree view control firstly we have to select the parent node for that child node. Then enter the name of the child node in a text box control and then click Add Child Node Button. How to use Tree View Control in C#

Code to Add Child Node

private void button2_Click(object sender, EventArgs e)

{

       if (treeView1.SelectedNode != null)//Check parent node is selected or not

            {

                if (textBox1.Text.Length != 0)//If child node name is entered.

                {

                    TreeNode childNode = new TreeNode(textBox1.Text);//Create an object of the child node and pass child node name to the constructor of Tree Node

                    treeView1.SelectedNode.Nodes.Add(childNode);//Add child node to the selected parent node

                    treeView1.ExpandAll();//Expand all the tree view control

                    textBox1.Clear();//clear the text box control.

                }

                else

                {//show the message to enter value in text box control if text box control is empty.

                    MessageBox.Show("Please Enter Value In The TextBox.", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }

            }

            else

            {//show the message to select node to which child node should be added.

                MessageBox.Show("Please Select Parent Node.", "Warning Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            }

}

Deleting Node in Tree View Control

To delete any node in tree view control firstly you have to select a node which needs to be deleted and then press Delete Node button.

How to use Tree View Control in C#

Code to Delete Child Node

private void button3_Click(object sender, EventArgs e)

{

            if (treeView1.Nodes.Count > 0)//Check whether tree view contains any node or //not.

            {

                if (treeView1.SelectedNode != null)//Check whether any node in tree view //control is selected or not.

                {

                    treeView1.SelectedNode.Remove();//If node is selected the remove that //node.

                    MessageBox.Show("Node Removed Successfully", "Success Message", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);//Show the success message to //that node.

                }

                else

                {//else show message to select any node.

                    MessageBox.Show("Please Select Any Node To Be Deleted.", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);

                }

            }

            else

            {//else show message that tree view control is empty.

                MessageBox.Show("There Is No Node In The Tree View Control.", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);

            }

}


Tree view d

By suresh puligadda on   5 years ago
Hai Sir,


 Thank You Sir, above Project details is very useful us.
I wanna Database connection to the above Project  like Tree view.
please send me sir.

Tree view control demo.

By Awadhendra Tiwari on   5 years ago

Hi Suresh,

You can easily bind records from database to a tree view control. Here I will give you some steps which might be useful

for you.

  1. Create two table namely Compony and Technology.
  2. Design user interface of your project.
  3. At the load-event of form establish connection with database.
  4. Store value of compony and technology in List<string []> collection.
  5. Then finally with appropriate condition display these records on tree view.

I hope these steps are helpful for solving your problem. If still you face some problem while solving this issue then you can can ask this question in details with your code on mindstick forum.

Thanks.


Don't want to miss updates? Please click the below button!

Follow MindStick