Hi vereato,
Refer below example. Use TreeView NodeMouseClick to achieve this.
Namespaces
CS
using System.Linq;
using System.Data;
VB.Net
Imports System.Linq
Imports System.Data
Code
Form1
CS
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.CenterToScreen();
treeView1.Nodes.Add(new TreeNode { Text = "Canada", Tag = "1" });
treeView1.Nodes.Add(new TreeNode { Text = "USA", Tag = "2" });
treeView1.Nodes.Add(new TreeNode { Text = "India", Tag = "3" });
}
private void OnNodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
TreeNode selectedNode = e.Node;
int countryId = Convert.ToInt32(selectedNode.Tag);
List<TreeNode> cities = this.GetCities().AsEnumerable()
.Where(x => x.Field<int>("CountryId") == countryId)
.Select(x => new TreeNode { Text = x["Name"].ToString(), Tag = x["Id"].ToString() })
.ToList();
Form2 frm2 = new Form2(cities); // Open Cities in new Form.
frm2.ShowDialog(this);
}
private DataTable GetCities()
{
DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("CountryId", typeof(int));
dt.Rows.Add(1, "Toronto", 1);
dt.Rows.Add(2, "Quebec", 1);
dt.Rows.Add(3, "New York", 2);
dt.Rows.Add(4, "Los Angeles", 2);
dt.Rows.Add(5, "Mumbai", 3);
dt.Rows.Add(6, "Bangalore", 3);
dt.Rows.Add(7, "Delhi", 3);
dt.Rows.Add(8, "Hyderabad", 3);
return dt;
}
}
VB.Net
Public Class Form1
Public Sub New()
InitializeComponent()
Me.CenterToScreen()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.CenterToScreen()
treeView1.Nodes.Add(New TreeNode With {.Text = "Canada", .Tag = "1"})
treeView1.Nodes.Add(New TreeNode With {.Text = "USA", .Tag = "2"})
treeView1.Nodes.Add(New TreeNode With {.Text = "India", .Tag = "3"})
End Sub
Private Sub OnNodeMouseClick(sender As Object, e As TreeNodeMouseClickEventArgs) Handles treeView1.NodeMouseClick
Dim selectedNode As TreeNode = e.Node
Dim countryId As Integer = Convert.ToInt32(selectedNode.Tag)
Dim cities As List(Of TreeNode) = Me.GetCities().AsEnumerable() _
.Where(Function(x) x.Field(Of Integer)("CountryId") = countryId) _
.Select(Function(x) New TreeNode With {
.Text = x("Name").ToString(),
.Tag = x("Id").ToString()
}).ToList()
Dim frm2 As Form2 = New Form2(cities) ' Open Cities in new Form.
frm2.ShowDialog(Me)
End Sub
Private Function GetCities() As DataTable
Dim dt As DataTable = New DataTable()
dt.Columns.Add("Id", GetType(Integer))
dt.Columns.Add("Name", GetType(String))
dt.Columns.Add("CountryId", GetType(Integer))
dt.Rows.Add(1, "Toronto", 1)
dt.Rows.Add(2, "Quebec", 1)
dt.Rows.Add(3, "New York", 2)
dt.Rows.Add(4, "Los Angeles", 2)
dt.Rows.Add(5, "Mumbai", 3)
dt.Rows.Add(6, "Bangalore", 3)
dt.Rows.Add(7, "Delhi", 3)
dt.Rows.Add(8, "Hyderabad", 3)
Return dt
End Function
End Class
Form2
CS
public partial class Form2 : Form
{
private List<TreeNode> cities;
public Form2(List<TreeNode> nodes)
{
InitializeComponent();
cities = nodes;
this.CenterToScreen();
}
private void Form2_Load(object sender, EventArgs e)
{
treeView1.Nodes.Clear();
treeView1.Nodes.AddRange(cities.ToArray());
}
}
VB.Net
Public Class Form2
Private cities As List(Of TreeNode)
Public Sub New(nodes As List(Of TreeNode))
InitializeComponent()
cities = nodes
Me.CenterToScreen()
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
treeView1.Nodes.Clear()
treeView1.Nodes.AddRange(cities.ToArray())
End Sub
End Class