471,325 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

Sort DataTable - C#


private DataTable SortDataTable(DataTable GetDataTable, string sort)
{
DataTable _NewDataTable = GetDataTable.Clone();
int rowCount = GetDataTable.Rows.Count;

DataRow[] foundRows = GetDataTable.Select(null, sort);
// Sort with Column name
for (int i = 0; i < rowCount; i++)
{
object[] arr = new object[GetDataTable.Columns.Count];
for (int j = 0; j < GetDataTable.Columns.Count; j++)
{
arr[j] = foundRows[i][j];
}
DataRow data_row = _NewDataTable.NewRow();
data_row.ItemArray = arr;
_NewDataTable.Rows.Add(data_row);
}

//Clear the incoming GetDataTable
GetDataTable.Rows.Clear();

for (int i = 0; i < _NewDataTable.Rows.Count; i++)
{
object[] arr = new object[GetDataTable.Columns.Count];
for (int j = 0; j < GetDataTable.Columns.Count; j++)
{
arr[j] = _NewDataTable.Rows[i][j];
}

DataRow data_row = GetDataTable.NewRow();
data_row.ItemArray = arr;
GetDataTable.Rows.Add(data_row);
}
return _NewDataTable;
}


Jun 27 '08 #1
0 908

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by ETX | last post: by
6 posts views Thread by Hardy Wang | last post: by
2 posts views Thread by Fredrik Rodin | last post: by
1 post views Thread by troyblakely | last post: by
5 posts views Thread by Gene Hubert | last post: by
1 post views Thread by Lyners | last post: by
3 posts views Thread by gilly3 | last post: by
3 posts views Thread by Bimal Kothari | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.