473,585 Members | 2,657 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 83139
Joe
You're sorting the DefaultView not the DataTable. If you set a datagrid to
DataTable.Defau ltView you should see the records are sorted.

-Joe

"Necqui Teja" <Ne*****@nospam .nospam> wrote in message
news:eW******** ******@tk2msftn gp13.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******** ******@tk2msftn gp13.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.mach in AT dot.state.fl.us > wrote
in message news:ex******** ******@TK2MSFTN GP14.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******** ******@tk2msftn gp13.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******** ******@TK2MSFTN GP10.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
9282
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 from select statement of sql query) in memory? Thanks -- WWW: http://hardywang.1accesshost.com ICQ: 3359839
3
1646
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 easily. Million thanks
5
2222
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 provided by clicking on a column header. By "Active Sort", I mean that editing a value in a sorted column may cause the record to be moved to maintain...
1
1976
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 process, behind the scenes, I combine the 2 datagridas into a datatable, sort and do processing for creating a load into another system. My problem...
6
3341
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 good for viewing it sorted, but I need it to actually _be_ sorted, and testing seems to show that the view doesn't do the trick. If I iterate through...
3
8277
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
1213
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 SqlClient.SqlConnection("Data Source=BICU111AA\SQLEXPRESS;Initial Catalog=ProteinDatabase;Integrated Security=True") SQLCon.Open() Dim...
0
983
by: Bimal Kothari | last post by:
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;
3
4996
by: Bimal Kothari | last post by:
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;
0
7908
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7836
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8336
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7950
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6606
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5389
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
1175
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.