Our 'series' of tips and tricks for Telerik Reporting continues with this neat approach to sort the report by column. We would create a sample report using the AdventureWorks database that comes with our examples and use a single table with few columns that are easy to remember namely FirstName, LastName, Title .. you know the drill :)
In order to use the built-in parameters area of the report, we would need to create a report Parameter that lists the column names. We can do that by creating a Business object that holds this for us:
public class ColumnSelector : DataTable
this.Rows.Add("Email Address", "EmailAddress");
this.Rows.Add("Not Sorted", "Not Sorted");
The last row is added simply for the sake of being able to show the report data unsorted if needed. We set the DisplayMember property of the parameter to displayText column and ValueMember to sortColumn column. Of course we should not forget to set the Visible property of the Report Parameter to true in order to show the parameter area above the ReportViewer.
Now is time to set the actual sorting:
- click on the Sorting property ellipsis to open the "Edit Sorting" editor
- click on New and select Expression from the Expression column
- enter the sorting criteria - in our case we need to check what is selected from the Report Parameter's combobox and sort by the respective column i.e.
= IIf(Parameters.SortByColumn = "Title", Fields.Title, 1)
- we do this for all columns we want to sort on and we're done :)
For your convenience, I have attached a sample project that incorporates the functionality in question. Enjoy!