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); } } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)