Solved

sorting Datatable using Dataview

Posted on 2008-06-10
4
7,620 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

772 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

10 Experts available now in Live!

Get 1:1 Help Now