C# Crystal Report Sorting

S

syed shah

sorts Crystal Report fields programatically.

In this example we can sort our report programatically.
I search lots of examples on internet how can we sort our report
programatically. But these all reports reply show's that examples have
errors or code genrate Invalid index. (Exception from HRESULT:
0x8002000B (DISP_E_BADINDEX)).
After applying and use different way's i can find one way to make
possible porgramatically sorting.
I made that code in C# with using Crystal reports.

SortFields sortFields = m_Report.DataDefinition.SortFields;
sortFields[0].Field = m_Report.Database.Tables
.Fields[field];
sortFields[0].SortDirection =
CrystalDecisions.Shared.SortDirection.AscendingOrder;

In this code,In First line we create a one sort object and its name is
sortFields by using sortFields class. In first line m_Report is my
report name.
In Second line sortFields[0].field is our object that take the
information of your database table and its field according to sort.
m_Report.Database.Tables
.Fields[field] in this code have your
table and its sort field number.
for example,
You have 3 table in your database, Customer, salesman, Product. You can
give choice to user it will be sort the report according to customer
name, saleman name or product name. according to my code Customer table
= 0 by table index in my database, salesman table = 1 and product table
= 2.
for example customer_name field in Customer table on location number 1.
same like index customer_no = 0, Customer_name = 1, Customer_address = 2
and so on
so i can set programatically set that number in my code.
i have 2 variables
int table;
int field;

variable table = (table number according to database)
variable field = (field number according to your table index)
so after movinh my table and field number in these variables i can
write that second line of my code with these 2 variables in my code.
sortFields[0].Field = m_Report.Database.Tables
.Fields[field];

my third line set the sort direction on my sort field.
sortFields[0].SortDirection =
CrystalDecisions.Shared.SortDirection.AscendingOrder;

this line set my sorting order we can chage it by
Ascending
Descending
BottomNorder
TopNorder

Thats it and after the your report viewer with your report Source.
Like that!
crystalReportViewer1.ReportSource = m_Report;

This 3 line code in C# make to allow to user sort according to different
report fields.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top