-
DataTable sorting
Hi guys,
This seems like a simple problem but I'm having difficulty finding the most efficient method. I have a DataTable with a list of comments (each row represents a comment). There can be replies to comments, so obviously I have to keep track of parent and child comments some how. So what I have is an id column and a parent_id column. the parent_id column points to the parent row if the comment is a reply obviously (0 if not).
I want to reorder my DataTable so that the comments are in order so I can display them on my page. I can change the DB structure if need and you think theres a better way butt hat my setup right now and I'm kinda stuck as to how to efficiently reorder my DataTable without having to call the DB again.
if you need any clarification. let me know.
Thanks in advance,
Boomba
-
From http://www.csharpfriends.com/Forums/...x?PostID=36859
Code:
DataRow[] foundRows = dt.Select(null, sort); // Sort with Column name
for (int i = 0 ; i < rowCount; i++)
{
object[] arr = new object[dt.Columns.Count];
for (int j = 0; j < dt.Columns.Count; j++)
{
arr[j]=foundRows[i][j];
}
DataRow data_row = newDT.NewRow();
data_row.ItemArray=arr;
newDT.Rows.Add(data_row);
}
//clear the incoming dt
dt.Rows.Clear();
for(int i = 0; i < newDT.Rows.Count; i++)
{
object[] arr = new object[dt.Columns.Count];
for (int j = 0; j < dt.Columns.Count; j++)
{
arr[j]=newDT.Rows[i][j];
}
DataRow data_row = dt.NewRow();
data_row.ItemArray = arr;
dt.Rows.Add(data_row);
}
}