Print self-adhesive labels from within a Lightswitch project. The labels I need are on pages in A4 size, 70mm wide and 35 mm high, 8 rows of labels and 3 columns of labels.
- The DevExpress ExtraReports extension for LightSwitch is installed. to use it in a LS project you need to add the Extension to you project. In the project properties, select the extension tab and check the DeveExpress XtraReports extension.
- Add the XtraReports as a WCF RIA data source.
- Right click on the Data sources folder, select ‘Add Data Source…’
- Choose WCF RIA Service, then ‘Next’ button.
- Select the XtraReportsService, then click ‘Next’
- Check ‘Entities’ and click ‘Finish’.
- Add a Data Source connected to the AdventureWorks database, I will use the tables Product, ProductSubCategory and ProductCategory. Product is related to ProductSubCategory which is in turn related to ProductCategory.
- To print the labels I add a query on the Products entity with a filter on the CategoryId, this query is called qryProductsbyCategory. The query uses a Where statement on ProductSubCategory.ProductCategory.ProductCategoryId, wich has to be equal to a parameter ProductCategoryId of type int (not optional).
Add the report
- In the solution explorer select ‘File View’
- In the solution explorer, right click on ‘Server, select ‘Add / New Item…’
- Select the XtraReport Class item, and click Add.
- A c-sharp class XtraReport1.cs and references will be added, a designer window and Xtrareports toolbox windows are opened.
- You can rearrange the Report explorer, Field list and group and sort window.
- In the Solution explorer, right click on XtraReport1.cs and rename it to LabelProductsByCategory.cs
- Bind the report to the query
- click the smart tag in the upper left corner of the report design window (see red marker in screenshot). This will open the ‘Report Tasks’ menu.
- In the DataSource combobox selec qryProductByCategory
- An object called LightSwitchDataSource1 is added at the bottom of the design screen.
- Design the label report layout
- Change the units from inches to millimeters (TenthsOfAMillimeter)
- click the smart tag and. In the ‘Report Tasks’ menu, select ‘Design in Report Wizard…’, select ‘Label report’, click ‘Next’
- In the Label Information dialog, you will have to select the proper label for your purpose. For my purpose 3 columns of labels 70mm wide and 8 rows of labels 35 mm high on a sheet size A4, there is the Zweckform type 3422.
- In the next screen you can correct the labels dimensions, as the dimensions are a bit off. 701.04 must be 700, 350.52 must be 350. Click finish.
- In the report design window, we see the report layout ready to add fields.
- First of all, remove the line around the xrPanel1 control, this is the rectangular box that lays over the text ‘Place controls here to keep them together’. To select this panel you need the Pointer tool from the Toolbox, DX11.1 Report controls.
- select pointer
- select xrPanel1, notice the selection handles.
- In the Properties panel, the Boreders property of xrPanel1 must be set to ‘None’
- Add fields to the Label, from the Field List pane, drag the Name and ProductNumber field on xrPanel1.
- Rearrange the width and height.
- As this is a label, I do not want to make the label grow in height to accommodate the text of a long name. I have made the Name label high enough to accommodate to lines of text. Click on the smart tag of the Name label, uncheck the CanGrow option. WordWrap is left on.
- Change the font size of the Name label. This can be done in the Font properties of the Name label. Click on the button with 3 dots … and in the font dialog set the size to 12 points.
- Sort the report on the productname. In the Group and sort window, click on Add a sort
- Save the report, build the solution.
Add a preview screen for the report
- In Solution explorer, switch back to Logical view.
- Right click on the Screens folder in the LS project. Select Add screen…
- In the Add new screen dialog window, select the Report preview Screen template and change the screen name to LabelProductsByCategory.
- The Application designer window shows the new screen. Click the Write Code button, select LabelProductsByCategory_Activated.
- In the Activated method, a line of code for the reportname is added by default, replace it with the correct name of the report.
Debug and view the report.
- The report preview screen shows a parameters numeric updown box (because the parameter is and integer).
- Enter value of 2 to show products of category 2. Click Submit.
- Reminder: As can be seen in the preview: if names are wider than the width of the label control, the name will be continued on a second line. since autogrow is off, the name control does not grow and therefore the number control does not move down to the next adhesive label.
XtraReports for LightSwitch can be used to print self-adhesive labels. It is fairly easy to create such a report in a minimal amount of time.
In a follow-up article I will describe how to add a lookup list with names for the product categories instead of the numeric updown box.