Hi vsaklani,
I have created a sample which full fill your requirement you need to modify the code according to your need.
I have taken reference from below article.
HTML
<div>
<table>
<tr>
<td>
<asp:TreeView ID="TreeView1" runat="server" ImageSet="XPFileExplorer" NodeIndent="15"
OnSelectedNodeChanged="TreeView1_SelectedNodeChanged">
<HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
<NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" HorizontalPadding="2px"
NodeSpacing="0px" VerticalPadding="2px"></NodeStyle>
<ParentNodeStyle Font-Bold="False" />
<SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="0px"
VerticalPadding="0px" />
</asp:TreeView>
</td>
<td>
<asp:TreeView ID="TreeView2" runat="server" ImageSet="XPFileExplorer" NodeIndent="15">
<HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
<NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" HorizontalPadding="2px"
NodeSpacing="0px" VerticalPadding="2px"></NodeStyle>
<ParentNodeStyle Font-Bold="False" />
<SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="0px"
VerticalPadding="0px" />
</asp:TreeView>
</td>
</tr>
</table>
</div>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.PopulateTreeView(this.GetData("SELECT Id, Name FROM VehicleTypes"), 0, null);
}
}
private void PopulateTreeView(DataTable dtParent, int parentId, TreeNode treeNode)
{
foreach (DataRow row in dtParent.Rows)
{
TreeNode child = new TreeNode { Text = row["Name"].ToString(), Value = row["Id"].ToString() };
if (parentId == 0)
{
TreeView1.Nodes.Add(child);
PopulateTreeView(this.GetData("SELECT Id, Name FROM VehicleSubTypes WHERE VehicleTypeId = " + child.Value), int.Parse(child.Value), child);
}
else
{
treeNode.ChildNodes.Add(child);
}
}
}
private DataTable GetData(string query)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
return dt;
}
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
try
{
string parent = this.TreeView1.SelectedNode.Parent.Text;
int parentId = Convert.ToInt32(this.TreeView1.SelectedNode.Parent.Value);
string child = this.TreeView1.SelectedNode.Text;
int childId = Convert.ToInt32(this.TreeView1.SelectedNode.Value);
DataTable dtParent = new DataTable();
dtParent.Columns.AddRange(new DataColumn[2] { new DataColumn("Id", typeof(int)), new DataColumn("Name") });
dtParent.Rows.Add(parentId, parent);
DataTable dtChild = new DataTable();
dtChild.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)), new DataColumn("Name"), new DataColumn("parentId") });
dtChild.Rows.Add(childId, child, parentId);
DuplicatePopulateTreeView(dtParent, dtChild);
}
catch (Exception ex)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", "alert('please select child node for copying')", true);
}
}
private void DuplicatePopulateTreeView(DataTable dtParent, DataTable dtChild)
{
for (int i = 0; i < dtParent.Rows.Count; i++)
{
TreeNode parentNode = new TreeNode { Text = dtParent.Rows[i]["Name"].ToString(), Value = dtParent.Rows[i]["Id"].ToString() };
foreach (DataRow childRows in dtChild.Rows)
{
parentNode = TreeView2.FindNode(dtParent.Rows[i]["Id"].ToString());
if (parentNode == null)
{
parentNode = new TreeNode { Text = dtParent.Rows[i]["Name"].ToString(), Value = dtParent.Rows[i]["Id"].ToString() };
TreeView2.Nodes.Add(parentNode);
}
TreeNode childNode = new TreeNode { Text = childRows["Name"].ToString(), Value = childRows["Id"].ToString() };
if (!NodeExists(parentNode, childRows["Name"].ToString()))
{
parentNode.ChildNodes.Add(childNode);
}
}
}
}
private bool NodeExists(TreeNode node, string key)
{
foreach (TreeNode subNode in node.ChildNodes)
{
if (subNode.Text == key)
{
return true;
}
}
return false;
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.PopulateTreeView(Me.GetData("SELECT Id, Name FROM VehicleTypes"), 0, Nothing)
End If
End Sub
Private Sub PopulateTreeView(dtParent As DataTable, parentId As Integer, treeNode As TreeNode)
For Each row As DataRow In dtParent.Rows
Dim child As New TreeNode() With { _
.Text = row("Name").ToString(), _
.Value = row("Id").ToString() _
}
If parentId = 0 Then
TreeView1.Nodes.Add(child)
PopulateTreeView(Me.GetData("SELECT Id, Name FROM VehicleSubTypes WHERE VehicleTypeId = " + child.Value), Integer.Parse(child.Value), child)
Else
treeNode.ChildNodes.Add(child)
End If
Next
End Sub
Private Function GetData(query As String) As DataTable
Dim dt As New DataTable()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand(query)
Using sda As New SqlDataAdapter()
cmd.CommandType = CommandType.Text
cmd.Connection = con
sda.SelectCommand = cmd
sda.Fill(dt)
End Using
End Using
Return dt
End Using
End Function
Protected Sub TreeView1_SelectedNodeChanged(sender As Object, e As EventArgs)
Try
Dim parent As String = Me.TreeView1.SelectedNode.Parent.Text
Dim parentId As Integer = Convert.ToInt32(Me.TreeView1.SelectedNode.Parent.Value)
Dim child As String = Me.TreeView1.SelectedNode.Text
Dim childId As Integer = Convert.ToInt32(Me.TreeView1.SelectedNode.Value)
Dim dtParent As New DataTable()
dtParent.Columns.AddRange(New DataColumn(1) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name")})
dtParent.Rows.Add(parentId, parent)
Dim dtChild As New DataTable()
dtChild.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name"), New DataColumn("parentId")})
dtChild.Rows.Add(childId, child, parentId)
DuplicatePopulateTreeView(dtParent, dtChild)
Catch ex As Exception
ClientScript.RegisterClientScriptBlock(Me.[GetType](), "Alert", "alert('please select child node for copying')", True)
End Try
End Sub
Private Sub DuplicatePopulateTreeView(dtParent As DataTable, dtChild As DataTable)
For i As Integer = 0 To dtParent.Rows.Count - 1
Dim parentNode As New TreeNode() With { _
.Text = dtParent.Rows(i)("Name").ToString(), _
.Value = dtParent.Rows(i)("Id").ToString() _
}
For Each childRows As DataRow In dtChild.Rows
parentNode = TreeView2.FindNode(dtParent.Rows(i)("Id").ToString())
If parentNode Is Nothing Then
parentNode = New TreeNode() With { _
.Text = dtParent.Rows(i)("Name").ToString(), _
.Value = dtParent.Rows(i)("Id").ToString() _
}
TreeView2.Nodes.Add(parentNode)
End If
Dim childNode As New TreeNode() With { _
.Text = childRows("Name").ToString(), _
.Value = childRows("Id").ToString() _
}
If Not NodeExists(parentNode, childRows("Name").ToString()) Then
parentNode.ChildNodes.Add(childNode)
End If
Next
Next
End Sub
Private Function NodeExists(node As TreeNode, key As String) As Boolean
For Each subNode As TreeNode In node.ChildNodes
If subNode.Text = key Then
Return True
End If
Next
Return False
End Function
ScreenShot