I am a danish programmer living in Bangkok.
Read more about me @ rasmus.rummel.dk.
Webmodelling Home > ASP.NET > C# Utility Functions > DataTable Get Parent Path

DataTable Get Parent Path


  • Example
    • Breadcrumb menus are an often used navigational construct typically implemented as a node path from top level node to current node, so typically you have a current node ID and have to find the path up to the top level node (or to home). I use this GetParentPath in my Discussions WebControl to implement such a category breadcrumb so people can see exactly there they are.
  • Example Code
    • DataTable myTable = GetData(); //your custom function
      DataTable dtParentPath = Utils.DataTable.GetParentPath(myTable, 8, "ID""PID");
      dtParentPath now contains a row for each node starting from the top level node down to current node (with ID=8).

The GetParentPath function :

public static System.Data.DataTable GetParentPath(System.Data.DataTable pdtSource, int pIDValue, string pIDColumn, string pPIDColumn)
	System.Data.DataTable dtResult = pdtSource.Clone();
	pdtSource.PrimaryKey = new DataColumn[] { pdtSource.Columns[pIDColumn] };
	DataRow drCurrent = pdtSource.Rows.Find(pIDValue);
	int PID = drCurrent.IsNull(pPIDColumn) ? 0 : (int)drCurrent[pPIDColumn];
	while (PID > 0)
		DataRow drParent = pdtSource.Rows.Find(PID);
		PID = drParent.IsNull(pPIDColumn) ? 0 : (int)drParent[pPIDColumn];
	System.Data.DataTable dtResultSortFromTop = dtResult.Clone();
	for (int r = dtResult.Rows.Count - 1; r >= 0; r--)
	return dtResultSortFromTop;


You can comment without logging in
 B  U  I  S 
Words: Chars: Chars left: