Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my code


C#
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;
using System.Data.SqlClient;

namespace treeview_add_delte
{
    public partial class Form1 : Form
    {
       
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void btnparent_Click(object sender, EventArgs e)
        {

            //if textbox is not empty.
            if (textBox1.Text.Trim().Length != 0)
            {
                //create an object of //Tree Node class and pass node name to the constructor of Tree Node.
                TreeNode parentNode = new TreeNode(textBox1.Text);
                treeView1.Nodes.Add(parentNode);
                textBox1.Clear();
            }

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

        private void btnchaild_Click(object sender, EventArgs e)
        {    
            //Check parent node is selected or not
            if (treeView1.SelectedNode != null)
            {
                //If child node name is entered.
                if (textBox1.Text.Length != 0)
                {
                    //Create an object of the child node and pass child node name to the constructor of Tree Node
                    TreeNode childNode = new TreeNode(textBox1.Text);
                    //Add child node to the selected parent node
                    treeView1.SelectedNode.Nodes.Add(childNode);
                    treeView1.ExpandAll();
                    textBox1.Clear();
                }
                else
                {
                    MessageBox.Show("Please Enter Value In The TextBox.", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }

            }

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


            }
        }

        private void btndel_Click(object sender, EventArgs e)
        {
            //Check whether tree view contains any node or //not.
            if (treeView1.Nodes.Count > 0)
            {
                //Check whether any node in tree view //control is selected or not.
                if (treeView1.SelectedNode != null)
                {
                    //If node is selected the remove that //node.
                    treeView1.SelectedNode.Remove();
                   MessageBox.Show("Node Removed Successfully", "Success Message", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                }

                else
                {

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

                }

            }

            else
            {

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

    }
}




this is my table query.......

my database name is EmployeeDB

my table name is treenodes

SQL
parentNode nvarchar(50) Nulls
childNode  nvarchar(50) Nulls
Posted
Updated 14-Mar-12 1:03am
v3
Comments
ProEnggSoft 14-Mar-12 6:57am    
Edit: pre tag for C# code added - PES

1 solution

add a uniqueID to each row and a nullable ParentID. In that case a child can find it's parent.
 
Share this answer
 
Comments
rockpune 14-Mar-12 7:17am    
am newly learning plz tell me how cani do that
Herman<T>.Instance 14-Mar-12 7:30am    
table treenodes should have the next fields
treenodeID int identity field
parentNode nvarchar (null)
childNode nvarchar (null)
parentTreeNodeID int (null)

You then can see in the parentTreeNodeID which to which parent it belongs.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900