471,349 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,349 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
3 4871
what the hell you are trying to do here??

Nirosh

<Bimal Kothariwrote in message
news:20************************@yahoo.com...
>
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 #2
Nirosh wrote:
what the hell you are trying to do here??
Sort a DataTable?

Nirosh

<Bimal Kothariwrote in message
news:20************************@yahoo.com...
>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 #3
Hello Just pass your data table and sort expression to sort (Date
and Amount) columns

Ok

"Andrew Cooper" wrote:
Nirosh wrote:
what the hell you are trying to do here??

Sort a DataTable?

Nirosh

<Bimal Kothariwrote in message
news:20************************@yahoo.com...
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 #4

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
reply views Thread by XIAOLAOHU | 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.