blah blah blah is here! blah blah » Close

up0down
link

What I want to do is make a method that returns a new DataTable sorted by the first column in the table.
e.g. if the input table is this
id name
2 h
3 e
5 s
4 d
1 a
i should get a new table with
id name
1 a
2 h
3 e
4 d
5 s
I have tried using the DataTable.Select method but it does not seem to sort for some reason.

last answered 9 months ago

1 answers

up0down
link

Try this code
DataTable.DefaultView.Sort = "id asc";

up0down
link

This is what I have and it doesn't work. (sorry about the tabbing)
private DataTable sortTable(DataTable table){
DataTable t = null;
t = table.Copy();
t.DefaultView.Sort=t.Columns[0].ColumnName+" asc";
//output to console for debugging
for (int i=0; i<t.Rows.Count; i++){
Console.WriteLine();
for (int j=0; j<table.Columns.Count; j++)
Console.Write(table.Rows[i].ItemArray[j].ToString()+"\t");
}
return t;
}

up0down
link

i worked it out. i needed the [ ] around the column name so i changed from
t.DefaultView.Sort=t.Columns[0].ColumnName+" asc";
to
t.DefaultView.Sort="["+t.Columns[0].ColumnName+"] asc";
Is there a way to combine duplicate values. Average them or something.

up0down
link

You simply go row by row calculate the sume and after divide by number of rows.

up0down
link

I should be more clear. What happens is i have data that is like this:
x y
1 2
1 5
2 3
3 1
and i want to get something like this:
x y
1 3.5
2 3
3 1

up0down
link

are you loading your datatable from a database? If so, I would recommend that you do that sorting on the database end of things
ie.
select *
from mytable
order by ID ASC

up0down
link

I should be more clear. What happens is i have data that is like this:
x y
1 2
1 5
2 3
3 1
and i want to get something like this:
x y
1 3.5
2 3
3 1
No one seems to have any ideas. And no, i am loading from DataTable based on CSV file i imported - not database

up0down
link

// I wrote this code to sort DataTable (Tested) works fine
//By: Hussain Hyder Ali Khowaja
// Karachi, Pakistan (www.ssuet.edu.pk/~hussaina)
DataRow[] foundRows = dt.Select(null,dt.Columns[0].ColumnName); // Sort with Column name
for (int i = 0 ; i <= foundRows.GetUpperBound(0); i++)
{ object[] arr = new object[foundRows.GetUpperBound(0)];
for (int j = 0; j <= foundRows[i].ItemArray.GetUpperBound(0); j++)
{ arr[j]=foundRows[i][j]; }
DataRow data_row = newDT.NewRow();
data_row.ItemArray=arr;
newDT.Rows.Add(data_row);
}
//URL: www.hhalik.cjb.net
//Email: hhalik@yahoo.com
//Cellular: 0044 (0) 7951 434197

up0down
link

Here is a more complete answer. Reorders the datatable.
private static void SortDataTable(DataTable dt, string sort)
{
DataTable newDT = dt.Clone();
int rowCount = dt.Rows.Count;
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);
}
}

up0down
link

I'm a newbie to C#...and this sorting method is close to what I need...
except to sort on two columns (e.g. primary and secondary keys)
How do I make a sort expression for datatable.select() based on two columns?

up0down
link

dataview v=dt.defaultview;
v.sort="columnName DESC";
dt=v.toTable();

up0down
link

Hi Raju666,
Thanks a lot yaar, you just saved my day, I have been looking for the solution since morning, but I couldn't manage to get it. Thanks a lot again.

This post was imported from csharpfriends, if you have a similiar question please ask it again.

All previous members have been migrated, hope you enjoy the new platform!

Feedback