How To Insert Update Delete Selected Node From TreeView Using C#
In this C# Tutorial we will see How To Add Edit Remove The Selected TreeView Node Using TextBox Value On treeView AfterSelect To Get The Selected Node And 3 Button To Do The Insert, Update, Delete In CSharp Programming Language And Visual Studio Editor.
Project Source Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class TreeView_Add_Edit_Delete_Node : Form
{
public TreeView_Add_Edit_Delete_Node()
{
InitializeComponent();
}
// add
private void button1_Click(object sender, EventArgs e)
{
TreeNode node = new TreeNode(textBox1.Text);
try
{
treeView1.SelectedNode.Nodes.Add(node);
}
catch (Exception ex)
{
treeView1.Nodes.Add(node);
}
}
// edit
private void button2_Click(object sender, EventArgs e)
{
treeView1.SelectedNode.Text = textBox1.Text;
}
// get selected node
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
textBox1.Text = treeView1.SelectedNode.Text;
}
// delete selected node
private void button3_Click(object sender, EventArgs e)
{
treeView1.SelectedNode.Remove();
}
}
}