Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    APIs
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> C# >> Node Insertion in TreeView control in C#
Node Insertion in TreeView control in C#
Node Insertion in TreeView control in C#


by AVADHESH PATEL on 7/21/2012 5:02:16 PM

Views: 1734       Comments: 0

Node Insertion in TreeView control in C#

TreeView Control used for display hierarchical structured data e.g. XML. Here we can add new node in TreeView and also insert sub node in selected node.

Steps are given below:

Step1:- Take one windows Form and Design Form like below

Node Insertion in TreeView control in C#

Step 2:- write code on Form’s CS File

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace TreeView

{

    public partial class TreeView : Form

    {

        public TreeView()

        {

            InitializeComponent();

 

           

        }

 

        private void btnInsert_Click(object sender, EventArgs e)

        {

            if (txtNode.Text.Trim().Length == 0 && txtSubNode.Text.Trim().Length == 0)

            {

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

            }

 

            else if (txtNode.Text.Trim().Length != 0 && txtSubNode.Text.Trim().Length==0)

            {

                TreeNode parentNode = new TreeNode(txtNode.Text.Trim());//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.

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

            }

 

            else if (treeView1.SelectedNode != null && txtSubNode.Text.Trim().Length != 0)

            {

                TreeNode childNode = new TreeNode(txtSubNode.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

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

            }

 

            else if (txtNode.Text.Trim().Length == 0 && txtSubNode.Text.Trim().Length != 0)

            {

                //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);

            }

 

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

            }

        }

 

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)

        {

            //select parent node

            txtNode.Text = treeView1.SelectedNode.Text;

        }

    }

}

Output:-

Node Insertion in TreeView control in C#

Report Abuse Form
Reason:    
 

Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by AVADHESH PATELRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 4321
Advertisement
MindStick Cleaner
Advertise with Us
  
Copyright © 2013MindStick. All Rights Reserved.