Skip to main content Skip to footer

ActiveReports 7:"Dynamic Sorting" in Section Based Reports

Sorting is currently not supported in the Section Based Reports available within ActiveReports 7. The most common approach which a user has to take to sort a report is to use the Order By clause while defining the SQL query for the data source. This approach works well for a static sorting on a particular column. However, a user is still not able to interact with the report on the viewer and sort it once it is displayed. So what if user wants to sort any column of the report in Ascending/Descending order but only after it gets displayed on the Windows Viewer? Well thanks to the flexible structure of ActiveReports that with some code-tweaking, we can get what we are looking for. Following screenshots show, how the report looks with the sorting glyphs next to the column headers and how it changes once we sort on any column:

Now let me provide the details about the implementation. The sort symbols which we see in the viewer are actually hyperlinks in the report. We need to use hyperlinks, as the viewer control provides HyperLink event which allows a user to interact with the report after it is displayed.

The main implementation defines two properties Sort and Column for the ActiveReportsClass, to keep track of the column to be sorted and the type of sorting to be done. The column headings which contains the sort symbols are placed in the Page Header section of the report. As mentioned in the last paragraph, the sort symbols are hyperlinks, so once a user clicks on any one of them, the Hyperlink event of the viewer control is fired and the Sort and Column properties of the report are populated.


private void viewer1_HyperLink(object sender, GrapeCity.ActiveReports.Viewer.Win.HyperLinkEventArgs e)  
{  
  NewActiveReport1 rpt = new NewActiveReport1();  
  rpt.Column = e.HyperLink.Split(':')[0];  
  rpt.Sort = (e.HyperLink.Split(':')[1] == "Asc") ? "Desc" : "Asc";  
  rpt.Run();  
  viewer1.LoadDocument(rpt.Document);  
}  

Next step involves regenerating the report using the data based on the SQL query with Sort and Column properties defining the Sorting order. On the basis of the sort order (Ascending or Descending) the sort symbol associated with the columns also change thus letting the user know about the column on which the report is currently sorted.


private void NewActiveReport1_ReportStart(object sender, EventArgs e)  
{  
  OleDBDataSource ds = new OleDBDataSource();  
  ds.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Data\\NWIND.MDB;Persist Security Info=False";  
  ds.SQL = "select * from Customers order by " + Column + " " + Sort;  
  ((TextBox)pageHeader.Controls["txt" + Column + "Sort"]).Text = (Sort == "Asc") ? "^" : "v";  
  ((TextBox)pageHeader.Controls["txt" + Column + "Sort"]).HyperLink = Column + ":" + Sort;  
  this.DataSource = ds;  
}  

Refer to the attached samples for complete implementation. DynamicSorting_C# DynamicSorting_VB

MESCIUS inc.

comments powered by Disqus