473,322 Members | 1,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 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 4974
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ETX | last post by:
Hello, My question is about the datagrid sorting. I have a datatable that is filled up with the data retrieved from an SQL query.I use that datatable as source for my datagrid. I would like to...
6
by: Hardy Wang | last post by:
Hi, I have a DataSet, returned by a stored procudure, which has only on DataTable. At this step there is no sort in DataTable. Is there is a way I can do to sort DataRows in this DataTable (not...
2
by: Fredrik Rodin | last post by:
All, I've been looking around for a solution to my problem for a couple of days now. In short, here's my situation: 1. I'm getting a result from a component back as a datatable and I have...
1
by: troyblakely | last post by:
I am having trouble sorting a datagrid. I have read numerous posts on this and other lists, and tried most of the suggestions, but none of them have worked for me yet. I populate a dataset from two...
5
by: Gene Hubert | last post by:
I'm using the DefaultView from the Datasource for a DataGrid to present the data in a particular order. It seems that sorting in this way is an "Active Sort", as is the default sort that is...
1
by: Lyners | last post by:
I am trying to figure out the best way to do this (currently I am having a problem sorting). I have a vb.net program that contains 2 datagrids on a form for the end user. When the user is ready to...
3
by: gilly3 | last post by:
I have a column in a DataView that contains NaN. When I attempt to Sort on this column, I get: MESSAGE: Index was outside the bounds of the array. SOURCE: System.Data STACKTRACE: at...
3
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Are there any good built-in or availble functions that I can call to sort my dataset datatalbe based on 1 or more of the table's columns? -- Thanks.
4
by: sklett | last post by:
(I posted this in a databinding NG, but it's a VERY low traffic NG so I thought I would post here as well. I hope no one minds too much, if you do I'm sorry) I have a DGV that is bound to a...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.