By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,775 Members | 1,347 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,775 IT Pros & Developers. It's quick & easy.

Sort DataTable

P: n/a
I have UPC and ItemNo as columns in my DataTable dt.

How do I sort the columns in my method prior to return dt;

I tried the following but it doesn't seem to work.

dt.DefaultView.Sort = "UPC ASC";
return dt;

Necqui
Mar 27 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Joe
You're sorting the DefaultView not the DataTable. If you set a datagrid to
DataTable.DefaultView you should see the records are sorted.

-Joe

"Necqui Teja" <Ne*****@nospam.nospam> wrote in message
news:eW**************@tk2msftngp13.phx.gbl...
I have UPC and ItemNo as columns in my DataTable dt.

How do I sort the columns in my method prior to return dt;

I tried the following but it doesn't seem to work.

dt.DefaultView.Sort = "UPC ASC";
return dt;

Necqui

Mar 27 '06 #2

P: n/a
The sort is of the DataView, not the DataTable.
So for example if you were to assign the DefaultView (DataView) as the
DataSource of a DataGrid control, you would see the sort results. The
original DataTable will remain unchanged.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Necqui Teja" wrote:
I have UPC and ItemNo as columns in my DataTable dt.

How do I sort the columns in my method prior to return dt;

I tried the following but it doesn't seem to work.

dt.DefaultView.Sort = "UPC ASC";
return dt;

Necqui

Mar 27 '06 #3

P: n/a
Hi,

A datatable is not sortable itself, what you do is using one or more
DataView of the same table, in each DataView you can set the sort order as
needed.
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Necqui Teja" <Ne*****@nospam.nospam> wrote in message
news:eW**************@tk2msftngp13.phx.gbl...
I have UPC and ItemNo as columns in my DataTable dt.

How do I sort the columns in my method prior to return dt;

I tried the following but it doesn't seem to work.

dt.DefaultView.Sort = "UPC ASC";
return dt;

Necqui

Mar 27 '06 #4

P: n/a
How can I take a sorted DataView and move it to a new DataTable so that I
can iterate using the foreach loop? I'm not using DataSource/DataGrid in my
application.

Necqui
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:ex**************@TK2MSFTNGP14.phx.gbl...
Hi,

A datatable is not sortable itself, what you do is using one or more
DataView of the same table, in each DataView you can set the sort order as
needed.
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Necqui Teja" <Ne*****@nospam.nospam> wrote in message
news:eW**************@tk2msftngp13.phx.gbl...
I have UPC and ItemNo as columns in my DataTable dt.

How do I sort the columns in my method prior to return dt;

I tried the following but it doesn't seem to work.

dt.DefaultView.Sort = "UPC ASC";
return dt;

Necqui


Mar 28 '06 #5

P: n/a
Don't iterate over the DataTable; iterate over the DataView.

DataTables are always in the order in which rows were returned by the
data source. (Your other choice is to request the rows from your data
source in the order you want them.)

However, it's better to just iterate over the DataView rather than the
DataTable. Then you can have any sorting order you want.

Mar 28 '06 #6

P: n/a
Hi,

"Necqui Teja" <Ne*****@nospam.nospam> wrote in message
news:eg**************@TK2MSFTNGP10.phx.gbl...
How can I take a sorted DataView and move it to a new DataTable so that I
can iterate using the foreach loop? I'm not using DataSource/DataGrid in
my application.


You can iterate in the dataview:

foreach( DataRowView row in theView )
dosomethigwith( row)

you can access the row using DataRowView.Row , so if you have a method that
especificaly use a Row you can use

foreach( DataRowView row in theView )
dosomethigwith( row.Row )
In general all list controls (Datagrid, listview, etc ) can handle DataView
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Mar 29 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.