ProblemFor a selected period I need to show/print a report with a summary per category and with a grand total. The dates to select from are stored in the detail record, but no detail should be shown.
SolutionQuick and dirty: create a report with a detail section that will be hidden, create a group header with the totals, add a report footer with the grand totals.
The report uses a LS Query based on the entity: 2 parameters from date and to date. To facilitate gathering data from several tables, I will create a view that gathers de detailed data.
In this solution I will not use WCF RIA services, which could be a better solution, because the data could be filtered on the SQL server side. My quick and dirty solution will extract all details and group these details.
PrerequisitesThis example was made with the SQL Server 2008 AdventureWorks database. A LightSwitch project called AdventureWorksLS. The Datasource AdventureWorksData has been added to the project.
SQL server view
- On the SQL server database I will create view that contains the necessary detailed information to create a detail of each product sold. The orderdetails table contains the base information, other tables are joined to obtain product category data
- OrderDate to select the period in the entity query
- ProductCategory Id and Name to select on a category in the entity query
- This view will be created as Sales.uvSalesOrderDetail
- The ISNULL function and CAST statements are added to prevent LightSwitch from interpreting every non nullable field as part of the primary key, My primary key is SalesOrderDetailId, that is way I used the ISNULL function. It is not beautiful but it works.
The entity and the filter query
- To add the view, right click on the Data Source AdventureWorksData, select Update Datasource
- Select the view uvSalesOrderDetail (Sales)
- The inferred key warning comes up, Select continue, because the view was constructed with ISNULL and Cast expressions to set this straight.
- The entity is created
- Add a new query to the entity, right click on the entity uvSalesOrderDetails
- Rename ‘Query1’ to ‘qrySalesOrderDetails’
- In the query design window, click Add Filter (single)
- Create the filter ‘Where’ ‘OrderDate’ ‘Is Between’. 2 parameters have to be created now.
- Click on the parameter type dropdown, select @ Parameter
- Click the textbox for the parameter value, select Add New…
- In the parameter name textbox replace the name OrderDate by FromDate
- Now add the new parameter type in the ‘Is between … And ‘ parameter type dropdown, select @ parameter. In the textbox for the parameter value, select Add New… In the parameter name textbox replace the name by ToDate.
Create the report
- In the Solution explorer change the view from Logical to File view.
- In the solution explorer, right click on the Server project, select Add / New Item…
- Select the XtraReports V11.1, give the report object a name: RptCategoryProductSales
- The report design window is opened
- Click on the smart tag on the upper left corner, open the DataSource dropdown, select the Query uvSalesOrderDetails and set the ReportUnit to TenthsOfAMillimeter.
- In the Group and Sort panel, click on Add a Group, select ProductCategoryName
- The group is added with a group header by default. I will add the total sales per category in this header, As you can see, to add a footer, the Show Footer checkbox can be checked (not in this example)
- From the Field list drag the ProductCategoryName and the LineTotal to the GroupHeader1 on the design surface space
- The LineTotal needs to be a sum, not a single value. Click on the smart tag of the LineTotal label.
- Click on the Summary ellipsis button … The summary editor windows opens.
- In the Summary Running select Group
- Click on the Format string ellipsis button … In the Category select Number. Click on the Custom tab
- Click OK twice to close both dialogs.
- To hide the Detail section, there are 2 possibilities, either change the height of the detail section to 0 or set the Visibility property to False.
- Click on Save All. Build the solution.
Create a report preview screen
- In Solution Explorer switch back to logical view
- To use XtraReports in the LightSwitch project we need to add the XtraReports extension to the project. And add a DataSource to the WCF RIA services XtraReports.
- Double click on the Project Properties. In the Extensions tab, check the DevExpress XtraReports extension.
- right click on Data sources, select Add Data Source…
- In the Attach Data Source Wizard, select WCF RIA Services, click Next.
- On the next page, select the XtraReportService, click Next.
- On the next page, Check Entities, click Finish.
- In the project right click on Screens, select Add Screen…
- In the Add screen dialog, under Select a screen template, select Report Preview Screen. In the Screen Name, type ReportCategoryProductSales. Click OK.
- In the designer panel, click on Write Code, Select ReportProducCategorySales_Activated.
- In the code window, overwrite the report name with the name of the Server report class: RptCategoryProductSales
- Save all, Debug the project.