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

Usage

  • 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);
	dtResult.ImportRow(drCurrent);
 
	int PID = drCurrent.IsNull(pPIDColumn) ? 0 : (int)drCurrent[pPIDColumn];
	while (PID > 0)
	{
		DataRow drParent = pdtSource.Rows.Find(PID);
		dtResult.ImportRow(drParent);
 
		PID = drParent.IsNull(pPIDColumn) ? 0 : (int)drParent[pPIDColumn];
	}
 
	System.Data.DataTable dtResultSortFromTop = dtResult.Clone();
	for (int r = dtResult.Rows.Count - 1; r >= 0; r--)
	{
		dtResultSortFromTop.ImportRow(dtResult.Rows[r]);
	}
 
	return dtResultSortFromTop;
}

Comments

You can comment without logging in
Profile
Username
Password
Password
Email
Nickpic
Get notified on reply to own posts  (only works if you specify an email address)
Get notified on receiving a PM  (only works if you specify an email address)
Remember my username
Remember my password
signature
Words: Chars: Chars left: