LightSwitch Reporting - Showing Report Preview for Individual Records

DevExpress Data Blog
09 September 2011

This blog post provides a walk-through for the process of integrating XtraReports into your LightSwitch application to show a report preview for individual data records.

To solve this task, do the following.

1. Start a new LightSwitch application or open an existing one. Add a data source to the application. 

 Adding a data source to a LightSwitch application

In this example, we will create a Categories-Products table relation from a sample Northwind database to create a hierarchical report.

Choosing data objects in LightSwitch

2. Add a new screen to your application.

Adding a new screen in LightSwitch

Choose a List and Detail Screen and set its data source to NorthwindData.Categories.

Providing screen information in LightSwitch

3. Double-click Properties in the Solution Explorer and add an XtraReports extension to the application.

How to add Xtrareports extension to a LightSwitch application

4. Choose to add another data source in the Solution Explorer, but this time choose WCF RIA Service in the invoked wizard.

How to add a data source in LightSwitch

Then, choose XtraReportsService and click Next. On the last wizard page, check to select all available Entities and click Finish.

 Adding a reporting service in LightSwitch

Note: we do not recommend you changing the default data source name (XtraReportsServiceData), because of the risk that your application will not work.

5. Create a LightSwitch query, in which categories are selected based on an integer parameter. 

 Adding a query in LightSwitch

6. To add an XtraReport to the application, switch to the File View in the Solution Explorer, select Server and press CTRL+SHIFT+A.

In the invoked dialog, choose XtraReport Class v11.1 and click Add

7. Re-build the solution and bind the report to the created query. 

How to bind an XtraReport to a LightSwitch Query

To access the query's parameters, right-click lightSwitchDataSource1 and click Properties. Then, click the ellipsis button for the QueryParameters property.

Accessing LightSwitch Query properties in XtraReports

In the invoked dialog, disable the parameter's Visible property.

Changing a LightSwitch Query parameter's visibility in XtraReports.png

Then, adjust the report layout as required. To learn how you can create a master-detail report, see How to: Create a Master-Detail Report using Detail Report Bands.

 Adjusting the report layout to create a master-detail report in XtraReports

8. Switch back to the Logical View in the Solution Explorer and add another screen to the application.

At this time, select Report Preview Screen and click OK

Adding a report preview screen to a LightSwitch application

In the opened screen designer, click "Add Data Item...". In the invoked editor, choose Local Property, set its Type to Integer and Name to CategoryId.

How to add a local property in LightSwitch

After the local property is created, press F4 and enable its Is Parameter option in the invoked Properties window.

Making a local property a parameter in LightSwitch

9. Now, click Write Code to switch to the screen's code behind, and in the body of the CustomizeReportPreviewModel method, assign the value of the parameter that is passed to the report from the query to the value of the created local property.

namespace LightSwitchApplication {
    public partial class ReportPreviewScreen {
        public void CustomizeReportPreviewModel(DevExpress.Xpf.Printing.ReportPreviewModel model) {
            model.Parameters["CategoryID"].Value = CategoryId;
        }
        partial void ReportPreviewScreen_Activated() {
            this.ReportTypeName = "XtraReport1";
        }
    }
}

Namespace LightSwitchApplication
    Partial Public Class ReportPreviewScreen
        Public Sub CustomizeReportPreviewModel(ByVal model As DevExpress.Xpf.Printing.ReportPreviewModel)
            model.Parameters("CategoryID").Value = CategoryId
        End Sub
        Private Sub ReportPreviewScreen_Activated()
            Me.ReportTypeName = "XtraReport1"
        End Sub
    End Class
End Namespace

10. Switch back to the CategoriesListDetail screen designer and add a button (named ShowPrintPreview) to the command bar of the Categories list.

Handle its Execute method to pass the parameter name to the method that shows the report preview screen.

 How to handle the Execute method of a button in LightSwitch

In addition, you can check whether or not this value is null in the button's CanExecute method body.

namespace LightSwitchApplication {
    public partial class CategoriesListDetail {
        partial void ShowPrintPreview_Execute() {
            Application.ShowReportPreviewScreen(Categories.SelectedItem.CategoryID);
        }
        partial void ShowPrintPreview_CanExecute(ref bool result) {
            result = Categories.SelectedItem != null;
        }
    }
}

Namespace LightSwitchApplication
    Partial Public Class CategoriesListDetail
        Private Sub ShowPrintPreview_Execute()
            Application.ShowReportPreviewScreen(Categories.SelectedItem.CategoryID)
        End Sub
        Private Sub ShowPrintPreview_CanExecute(ByRef result As Boolean)
            result = Categories.SelectedItem IsNot Nothing
        End Sub
    End Class
End Namespace

Run the application, choose a category and click Show Print Preview.

 How to use XtraReports in LightSwitch

As a result, a report showing the category details is displayed in a separate screen.

LightSwitch reporting with XtraReports 


18 comment(s)
Stephen J Naughton
Stephen J Naughton

Nice to have a concise verion I had to keep wathcing Seth's video each time I did this not I have this thanks

Steve

13 September, 2011
teranga teranga
teranga teranga

Nice George

But How Can I do if i have 2 optionnal parameters in my lightswitch query

I don't say 2 parameters only but 2 optionnal Parameters

Thanks a lot for your help

18 October, 2011
George (DevExpress)
George (DevExpress)

@teranga: Do you mean Boolean parameters? If so, the following tutorial should give you an idea on how to pass them to your report: documentation.devexpress.com

19 October, 2011
teranga teranga
teranga teranga

I use your software for a month and I think I'm starting to feel comfortable.

I am not a beginner.

In lightswitch I do a query with a filter that has two parameters

which are optional but "xtrareport" don't consider  the option "optional"

look at the query designer of  lightswitch and you will understand my question

in the Properties screen there is an option "optional" for the parameters

19 October, 2011
George (DevExpress)
George (DevExpress)

Thank you, now I understand. At present XtraReports does not support optional parameters. Would you please make a corresponding request in our Support Center with more details about your task specifics?

19 October, 2011
teranga teranga
teranga teranga

Thanks a lot , George

19 October, 2011
Jim Waa
Jim Waa

George, Can you post the VB for this post?

23 October, 2011
George (DevExpress)
George (DevExpress)

@Jim: As you requested, I have added VB code in this blog post.

25 October, 2011
Pavel Pták
Pavel Pták

Hi George,

thanks for a really useful article. Is there any progress with optional parameters? If I send an optional parameter to DXReports, it translates it from "nothing" to 0 (in case of int data type) and the query returns no rows as there is no record with RecordID=0 (app logic is like if any record is selected, print just this one, if not, print all of them).

As a workaround I tried to create one query in LS without this optional parameter and another one based on the first one with additional parameter and then programmatically change DataSource property of the report data source (e.g. if MyParam=0 - attach it to one query, else attach it to another one).

I'm not sure if this is possible. If so, please, could you give me some hints how to do it?

Thanks in advance,

Pavel

12 February, 2012
George (DevExpress)
George (DevExpress)

@Pavel: Have you tried addressing this question to our Support Team (at http://devexpress.com/sc/)? I am sure they will find the best possible solution.

13 February, 2012
Alex Hind (DevExpress)
Alex Hind (DevExpress)

Hi Pavel,

At present, the issue regarding an optional parameter in LightSwitch is still active.

We plan to fix it in the context of the www.devexpress.com/scid=Q349885 suggestion.

Below you'll find details on how to work around this issue:

1) Add 2 queries to the datasource;

2) Assign the first query as a report datasource - a LightSwitchDataSource object will be added to the component container;

3) Assign the second query as a report datasource - one more LightSwitchDataSource object will be added to the component container;

4) Add an Int32 parameter to the report (parameter1);

5) Handle the LightSwitchReport.BeforePrint event to set the report datasource depending on the value parameter, for example:

   public partial class XtraReport1 : DevExpress.XtraReports.UI.LightSwitchReport {

       public XtraReport1() {

           InitializeComponent();

           BeforePrint += new System.Drawing.Printing.PrintEventHandler(XtraReport1_BeforePrint);

       }

       void XtraReport1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e) {

           if(parameter1.Value == 0)

               DataSource = lightSwitchDataSource1;

           else

               DataSource = lightSwitchDataSource2;

       }

   }

Now, on the client side you can set the 'parameter1' value to define which datasource will be used during report generating:

   public partial class ReportPreviewScreen {

       public void CustomizeReportPreviewModel(DevExpress.Xpf.Printing.ReportPreviewModel model) {

           model.Parameters["parameter1"].Value = 0; // use the first data source

           //model.Parameters["parameter1"].Value = 1; // use the second data source

       }

       partial void ReportPreviewScreen_Activated() {

           this.ReportTypeName = "LightSwitchApplication.XtraReport1";

       }

   }

Hope you will find this helpful.

Thanks,

Alexey

14 February, 2012
Pavel Pták
Pavel Pták

Hi Alex,

thanks a lot for a detailed explanation, it works like a charm!!!:-) Optional parameters will definitely be a nice to have feature, but with this workaround I can live without it for some (unspecified) time. It just needs a little extra effort.

Best regards,

Pavel

15 February, 2012
Louis Garcia
Louis Garcia

I've been trying to pass parameter to report with VS2012 Lightswitch using these steps, but have been unsuccessful.

When I do the steps above for my data, I get a transaction has aborted message.

I've been able to manually set a parameter on the report and run, but not using the print preview example shown here.

9 October, 2012
Ramin Rownaghi
Ramin Rownaghi

I have the same problem. I just imported my 2011 Report into new version and everything looks fine except it throws Transaction Aborted right at the time of running the report.

23 October, 2012
Israel Garcia 4
Israel Garcia 4

Could please help me with xtrareport and lightswitch 2012, I need to open a report from a search screen for each patient or client individually, I followed some articles published on the web until the DevExpress page but nothing works, at the time of open the report gives an error, the expression is not supported ... please i need help, nno apparently supported the value you do, someone could help me I will be very grateful

26 August, 2013
Israel Garcia 4
Israel Garcia 4

Could please help me with xtrareport and lightswitch 2012, I need to open a report from a search screen for each patient or client individually, I followed some articles published on the web until the DevExpress page but nothing works, at the time of open the report gives an error, the expression is not supported ... please i need help, nno apparently supported the value you do, someone could help me I will be very grateful

26 August, 2013
Israel Garcia 4
Israel Garcia 4

Could please help me with xtrareport and lightswitch 2012, I need to open a report from a search screen for each patient or client individually, I followed some articles published on the web until the DevExpress page but nothing works, at the time of open the report gives an error, the expression is not supported ... please i need help, apparently not supported value is done, someone could help me I will be very grateful

26 August, 2013
Israel Garcia 4
Israel Garcia 4

Could please help me with xtrareport and lightswitch 2012, I need to open a report from a search screen for each patient or client individually, I followed some articles published on the web until the DevExpress page but nothing works, at the time of open the report gives an error, the expression is not supported ... please i need help, apparently not supported value is done, someone could help me I will be very grateful

26 August, 2013

Please login or register to post comments.