Solved

sorting Datatable using Dataview

Posted on 2008-06-10
4
7,637 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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