473,396 Members | 1,913 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,396 software developers and data experts.

Sort DataTable

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
6 83127
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
3
by: Grey | last post by:
I have create a DataTable in DataSet and I want to have different sorting in this datatable from time to time. Is it possible to sort the data within the datatable, so i can sort the datagrid...
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...
6
by: David P. Donahue | last post by:
I've been looking around for ways to sort the rows in a DataTable, and everything seems to point to just changing the Sort property on that DataTable's DefaultView property. That's all well and...
3
by: Lowry Smith | last post by:
I am building the datatable (Name: dtCutomer) with Column1, Column2, Column3. What is the syntax to sort the data in a datatable on Column1. Vb.Net and .Nete 1.1. Thank you, Lowry
1
by: gyap88 | last post by:
Can someone add a statement to these few lines of code to help me sort the datatable in alphabetic order, taking the column "Sequence" as reference Dim dt As New DataTable() Dim SQLCon As New...
0
by: Bimal Kothari | last post by:
private DataTable SortDataTable(DataTable GetDataTable, string sort) { DataTable _NewDataTable = GetDataTable.Clone(); int rowCount = GetDataTable.Rows.Count; DataRow foundRows =...
3
by: Bimal Kothari | last post by:
private DataTable SortDataTable(DataTable GetDataTable, string sort) { DataTable _NewDataTable = GetDataTable.Clone(); int rowCount = GetDataTable.Rows.Count; DataRow foundRows =...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.