Solved

sorting Datatable using Dataview

Posted on 2008-06-10
4
7,632 Views
Last Modified: 2013-11-26
I need to sort a datatable only once and then iterate through its rows multiple times (after postbacks, so I am using a cache to store the datatable).

       DataTable dt = null;
       DataView dv = ((DataTable)Cache[User.Identity.Name]).DefaultView;
       dv.Sort = "date";
       dt = dv.Table;

At this point, will the datatable 'dt' have sorted rows?

Another thing, does the following statement always return the rows of the datatable in the same order, if I use it multiple times?
      foreach (DataRow dr in dt.Rows)

I would prefer not to use this:
     foreach (DataRowView drv in dt.DefaultView).

Thanks.
0
Comment
Question by:engg
  • 2
  • 2
4 Comments
 
LVL 33

Expert Comment

by:raterus
ID: 21752923
The DataView does not modify any data in the DataTable, it's truly just a "View" of your DataTable.

You should definitely iterate over the DataTable and not the DataView if you are concerned about the order.
0
 

Author Comment

by:engg
ID: 21752992
Thanks.

1)
DataTable dt = null;
DataView dv = ((DataTable)Cache[User.Identity.Name]).DefaultView;
dv.Sort = "date";
dt = dv.Table;

At this point, would the datatable 'dt' contain data sorted by date?


2)
If I call  this statement multiple times, will it always return the data in the datatable in the same order?
foreach (DataRow dr in dt.Rows)
{
.....
}
0
 
LVL 33

Accepted Solution

by:
raterus earned 500 total points
ID: 21753027
1) No, "Table" here is a just a reference to the underlying datatable.  If you want to create a new table of sorted information, us the .ToTable function of the DataView (.Net 2.0+ only)

e.g.
DataView dv = ((DataTable)Cache[User.Identity.Name]).DefaultView;
dv.Sort = "date";
DataTable dt = dv.ToTable();

2) Yes
0
 

Author Comment

by:engg
ID: 21753139
Thank you.
1) I didn't want to have an overhead of creating a new sorted table. But I think that's the only option.

2) Good. Because I thought if it works like a select query in SQL, it doesn't have to return the data in the same order.

I will get back soon.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now