Connecting Tech Pros Worldwide Forums | Help | Site Map

Sort DataTable

Necqui Teja
Guest
 
Posts: n/a
#1: Mar 27 '06
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



Joe
Guest
 
Posts: n/a
#2: Mar 27 '06

re: Sort DataTable


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" <NecquiT@nospam.nospam> wrote in message
news:eWojDAdUGHA.1576@tk2msftngp13.phx.gbl...[color=blue]
>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
>
>[/color]


Peter Bromberg [C# MVP]
Guest
 
Posts: n/a
#3: Mar 27 '06

re: Sort DataTable


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:
[color=blue]
> 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
>
>
>[/color]
Ignacio Machin \( .NET/ C# MVP \)
Guest
 
Posts: n/a
#4: Mar 27 '06

re: Sort DataTable


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" <NecquiT@nospam.nospam> wrote in message
news:eWojDAdUGHA.1576@tk2msftngp13.phx.gbl...[color=blue]
>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
>
>[/color]


Necqui Teja
Guest
 
Posts: n/a
#5: Mar 28 '06

re: Sort DataTable


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:exj1BIdUGHA.5552@TK2MSFTNGP14.phx.gbl...[color=blue]
> 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" <NecquiT@nospam.nospam> wrote in message
> news:eWojDAdUGHA.1576@tk2msftngp13.phx.gbl...[color=green]
>>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
>>
>>[/color]
>
>[/color]


Bruce Wood
Guest
 
Posts: n/a
#6: Mar 28 '06

re: Sort DataTable


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.

Ignacio Machin \( .NET/ C# MVP \)
Guest
 
Posts: n/a
#7: Mar 29 '06

re: Sort DataTable


Hi,

"Necqui Teja" <NecquiT@nospam.nospam> wrote in message
news:egoHesrUGHA.6112@TK2MSFTNGP10.phx.gbl...[color=blue]
> 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.[/color]

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


Closed Thread