?
Solved

sorting Datatable using Dataview

Posted on 2008-06-10
4
Medium Priority
?
7,643 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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 is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

719 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