Just use the Parent-Property of the TreeNode (if it's null there is no parent). So if you look for the "grandparent" just use Parent.Parent -> Look at this example: (Just copy to a new WindowsForms project and replcace Program.cs with the following:)
using System;
using System.Windows.Forms;
namespace TreeViewTest
{
static class Program
{
[STAThread]
static void Main()
{
Form form = new Form();
Label labelInfo = new Label();
labelInfo.Height = 50;
labelInfo.Dock = DockStyle.Top;
TreeView treeview = new TreeView();
treeview.Dock = DockStyle.Fill;
treeview.AfterSelect += delegate(object sender, TreeViewEventArgs e)
{
labelInfo.Text = String.Format("I'm {0}, my parent is {1}, my grandparent is {2}",
e.Node.Text,
e.Node.Parent != null ? e.Node.Parent.Text : "dead",
e.Node.Parent != null && e.Node.Parent.Parent != null ? e.Node.Parent.Parent.Text : "dead");
};
TreeNode nodeGrandParent = new TreeNode("Grandparent");
TreeNode nodeParentMom = new TreeNode("ParentMom");
TreeNode nodeParentDad = new TreeNode("ParentDad");
TreeNode nodeChild = new TreeNode("Child");
nodeGrandParent.Nodes.Add(nodeParentMom);
nodeGrandParent.Nodes.Add(nodeParentDad);
nodeParentMom.Nodes.Add(nodeChild);
treeview.Nodes.Add(nodeGrandParent);
form.Controls.Add(treeview);
form.Controls.Add(labelInfo);
Application.Run(form);
}
}
}