Export Grouped data to files while Report is being generated

It's an idea that is seldom used. Most of the Reporting implementations deal with export when the complete report has been generated, and while data is something that is played around with to get which ever type of reports are required. With C1Report, we can reverse the process and play around with report design and logic to get grouped data about to be rendered in the report exported to file (PDF's, RTF's, HTML files) even before the complete report is rendered. It may not be a scenario that is used much often, simply because of the report generation overhead, as the export and report rendering works simultaneously on the same engine. Here's how we can implement the same, 1) Designing of the Report: The most important function of a Report is to show data in the form that is interpretable easily, i.e. show data in the form that makes sense to the viewer. That said, I believe almost half of the Reporting issues are because of design defects rather than limitations of the Controls used or framework. For what we have to achieve the design is the most important factor. Everyone's used grouping in report now and then, so that is also what we would need if we need to access grouped data. But the twist comes when in the Detail section of the report we use a SubReport and not individual fields. So what ever fields and data the you would need in a grouped detail, we would be required to build a separate report file for that and use it in the main report. A sample of the same type of report is attached with the post. 2) Grouping : To keep the understanding simple, I have used the Nwind.mdb data base, Categories table for the main report and Products table for the SubReport. In the main report, create a group as categories, and in the GrouperHeader section add which ever caption fields you would need to show the data. I used "Category Name". In the main report's detail section the SubReport is added as a field and for that field's text property we can use the following code so that Product details are rendered as per the current Category, "[CategoryID] = '" & [CategoryID] & "'" Save the reports and that would end the designing part for you. 3) Exporting Logic: The export and rendering needs to be done through code, as you would already be doing that in your application that uses the C1Report control. I have created and attached a very simple sample that showcases the required code for such an implementation. Quite simple we need to tap the Detail section of the report while it is being generated and render the SubReport that we used separately with the same data that it would render in the current section. Confusing? Lets make that simple, In the StartSection event of C1Report, we check for the current section to be a GroupeHeader so that we know a category break is coming up, Code: if (e.Section == C1.C1Report.SectionTypeEnum.GroupHeader1) { try { CatID = i.ToString(); // "i" to be declared as a Global Variable so that it's value can be accessed outside this method's scope Console.WriteLine(CatID); } catch(Exception ex) { Console.WriteLine(ex.Message); } } Once the category count is obtained, we can use it to pass it on the SubReport as a filter in the datasource property to obtain the same set of grouped values. Code: if (e.Section == C1.C1Report.SectionTypeEnum.Detail) { C1.C1Report.Section newsection = c1Report1.Sections["Detail"]; newsection.Fields["Field1"].Subreport.DataSource.Filter = "CategoryID = " + CatID; if (newsection.Fields["Field1"].SubreportHasData) { newsection.Fields["Field1"].Subreport.RenderToFile(@"C:\Test"+i+".pdf", C1.C1Report.FileFormatEnum.PDF); } i++; } The above code would make sure that while the report is being generated, the grouped data gets exported. Please refer to the attached sample and reportdefinition file for more details. The above approach used is a very simplified one, just to make the logic for implementing this clear. It may be that your reports may not have a CategoryID for the grouping table, for that we need to come up with on the spot innovations :) Happy Reporting, Snehit Krishna Attachments, Report_ExportByGroup ExportGroups


GrapeCity Developer Tools
comments powered by Disqus