Solved

Sort Datatable in Crystal Report

Posted on 2010-09-08
10
844 Views
Last Modified: 2012-06-22
Hi

I have created a datatable with 10 lines of data in it.

I then sort the datatable.
dtResult.DefaultView.Sort = "avgmp desc";

I then bind the datatable to a datagrdview.
The results display correctly in a datagridview.

However, if I display this datatable in a Crystal Report the results are unsorted.

Why? and how do I order it for the report?
0
Comment
Question by:elmbrook
  • 7
  • 2
10 Comments
 
LVL 8

Expert Comment

by:kingjely
ID: 33633485

Hi there,
Put the field on the report,
Click the record sort expert  a-z button up the top..
Then add your {Table.avgmp} field in and choose 'Descending.'

Does this work?


0
 
LVL 8

Expert Comment

by:kingjely
ID: 33633500

Otherwize

Add your {Table.avgmp} field.

Then
Go to Group Expert, Add {Table.avgmp} field as a Group and choose 'Options' and Choose 'Origional Order'
Then righ click and suppress the group GH1#{Table.avgmp}.

Kj
0
 

Author Comment

by:elmbrook
ID: 33633524
Hi King

Yes that did work. Never realised you could sort it from the report... My bad.

However, Can I do this programically? Before I display the results I have 3 different sorting options for the user.
0
 
LVL 8

Accepted Solution

by:
kingjely earned 0 total points
ID: 33633578
Hi is okay,
I'm sure you can do it in C# but im not too sure how..

I do my sorting programatically in crystals,

By 3 different sort options, do you mean Asc, Desc, Orig order,
Or 3 different sort options fields, as in,  by UNITS, PRICE, DATE ?



0
 

Author Comment

by:elmbrook
ID: 33633593
Hi King

I figured it out myself.

The code to sort is below:
                FieldDefinition fd;
                fd = cryRpt.Database.Tables[0].Fields[_sortfield];
                cryRpt.DataDefinition.SortFields[0].Field = fd;
                cryRpt.DataDefinition.SortFields[0].SortDirection = CrystalDecisions.Shared.SortDirection.DescendingOrder;

Where _sortfield is my field to sort on. as in Units, Price, Date.

Thank you for pointing me in the right direction. I will credit the points to you anyway.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 12

Expert Comment

by:rgn2121
ID: 33633594
This is VB.Net code, but it should give you an idea of programmatically sorting...

'ReportDocument.SetParameterValue("Sort Criteria", "My Sort Field")
'Dim crSortField As CrystalDecisions.CrystalReports.Engine.SortField
'ReportDocument.DataDefinition.SortFields(1).SortDirection = CrystalDecisions.Shared.SortDirection.DescendingOrder
 You could do without the parameter if you aren't going to offer dynamic sorting to the end user.  I have a web link that talks about this if I can find it...
0
 
LVL 8

Expert Comment

by:kingjely
ID: 33633597

If you want to see how it works,
By 3 different sort options, do you mean Asc, Desc, Orig order,

Make a NEW parameter called {?SORTORDER}
'save and close'

Then
In your group expert where you have your group {Table.avgmp} go to options, check Use formular X2

put this formular in;

If {?SORT} = 'ASC' then crAscendingOrder else
if {?SORT} = 'DESC' then crDescendingOrder else
crOriginalOrder;

Refresh your report and type in ASC, or DESC or leave it blank.

So thats how you order it for the report  ;)  
0
 
LVL 8

Expert Comment

by:kingjely
ID: 33633601
Hi rgn2121 and i gave you the answers in VB and Crystals and u also got it,
I'm not sure if you ment to award No POint or not, but we did help!
0
 
LVL 8

Expert Comment

by:kingjely
ID: 33633607
If you ment to it's okay, atleast you got the answer for both now
0
 
LVL 8

Expert Comment

by:kingjely
ID: 33633671
haha no worries, is a bit wierd?!

Thanks,
Kj
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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!
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

18 Experts available now in Live!

Get 1:1 Help Now