Click here to Skip to main content
15,891,951 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
    {
        SqlConnection con = new SqlConnection("Data Source=CIODEV03\\SQLEXPRESS;Initial Catalog=EmployeeDB;Integrated Security=True");
        SqlCommand cmd = new SqlCommand();
        SqlDataReader dr;
        DataSet ds;

       
        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);
                //Adding parent node to a tree view.
                treeView1.Nodes.Add(parentNode);
                //Clear the text box control.
                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);
                    //Expand all the tree view control
                    treeView1.ExpandAll();
                    //clear the text box control.
                    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();
                    //Show the success message to //that node.
                    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);
            }
        }

    }
}
Posted
Updated 12-Mar-12 20:46pm
v2
Comments
OriginalGriff 13-Mar-12 3:25am    
This won't solve your problem, but...don't write comments that say exactly what the code does:

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

We can see that the code generates a new node, and that it accepts a text parameter - all you have done is written the same thing out in a longer (and less accurate) form.
Comments should add information - why this is being done - not duplicate information as there is a good chance that the comment will not be updated when the code changes...
Oshtri Deka 13-Mar-12 3:56am    
Give us table schema as well.

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