elmbrook
asked on
Sort Datatable in Crystal Report
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?
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?
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
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi King
I figured it out myself.
The code to sort is below:
FieldDefinition fd;
fd = cryRpt.Database.Tables[0]. Fields[_so rtfield];
cryRpt.DataDefinition.Sort Fields[0]. Field = fd;
cryRpt.DataDefinition.Sort Fields[0]. SortDirect ion = CrystalDecisions.Shared.So rtDirectio n.Descendi ngOrder;
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.
I figured it out myself.
The code to sort is below:
FieldDefinition fd;
fd = cryRpt.Database.Tables[0].
cryRpt.DataDefinition.Sort
cryRpt.DataDefinition.Sort
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.
This is VB.Net code, but it should give you an idea of programmatically sorting...
'ReportDocument.SetParamet erValue("S ort Criteria", "My Sort Field")
'Dim crSortField As CrystalDecisions.CrystalRe ports.Engi ne.SortFie ld
'ReportDocument.DataDefini tion.SortF ields(1).S ortDirecti on = CrystalDecisions.Shared.So rtDirectio n.Descendi ngOrder
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...
'ReportDocument.SetParamet
'Dim crSortField As CrystalDecisions.CrystalRe
'ReportDocument.DataDefini
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...
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 ;)
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!
I'm not sure if you ment to award No POint or not, but we did help!
If you ment to it's okay, atleast you got the answer for both now
haha no worries, is a bit wierd?!
Thanks,
Kj
Thanks,
Kj
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?