Skip to main content
A newer version of this page is available.

Lesson 5 - Pass Multi-Value Parameters to Reports

  • 5 minutes to read

This example illustrates the process of creating a reporting application where an end-user can provide multiple values for an individual report parameter.

To learn how to allow the passing of multi-value parameters to your LightSwitch reports, do the following.

  1. Create a LightSwitch project with the XtraReports extension enabled and add a WCF RIA service to the application (for more details, see Lesson 1 - Create a Data-Aware Report).
  2. Add a data source. In this example, we will create a LightSwitch data source (named NorthWindData) based on theProducts and Categories tables of the sample Northwind database.
  3. Add a query (named ProductsByCategories) to the Products table. In the created query, create a String parameter (named categories).

    LightSwitch - MultiValueParameters01

  4. In the same window, expand the Write Code drop-down list and click the ProductsByCategories_PreprocessQuery method link to add the following code.

    using System;
    using System.Linq;
    // ... 
    
    namespace LightSwitchApplication {
        public partial class NorthWindDataService {
            partial void ProductsByCategories_PreprocessQuery(string categories, ref IQueryable<Product> query) {
                if (!String.IsNullOrEmpty(categories)) {
                    var ids = Array.ConvertAll(categories.Split(';'), x => int.Parse(x));
                    query = query.Where(x => ids.Contains(x.Category.CategoryID));
                }
            }
        }
    }
    
  5. Add a new XtraReport to the Server part of the application, bind the report to the created query and adjust the report’s layout.

    LightSwitch - MultiValueParameters02

  6. Add a Report Preview screen, switch to its code and write the following logic in it.

    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Windows.Data;
    using DevExpress.Utils;
    using DevExpress.Xpf.Editors;
    using DevExpress.Xpf.Printing;
    // ... 
    
    namespace LightSwitchApplication {
        public partial class ReportPreviewScreen {
            class CategoryInfo {
                public int ID { get; set; }
                public string Name { get; set; }
                public override bool Equals(object obj) {
                    CategoryInfo other = obj as CategoryInfo;
                    return other != null && other.Name == Name && other.ID == ID;
                }
                public override int GetHashCode() {
                    return HashCodeHelper.CalcHashCode(Name, ID);
                }
            }
    
            class CategoryIDsToStringConverter : IValueConverter {
                public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
                    string categories = value as string;
                    if (string.IsNullOrEmpty(categories)) {
                        return null;
                    }
                    return ArrayHelper.ConvertAll(categories.Split(';'), x => int.Parse(x));
                }
    
                public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
                    IEnumerable<object> ids = value as IEnumerable<object>;
                    if (ids == null) {
                        return null;
                    }
                    return String.Join(";", ids);
                }
            }
    
            // Do not rename the CustomizeReportPreviewModel method  
            // because it is used to access the ReportPreviewModel that is associated with this Report Preview Screen. 
            // You can remove this method if you do not need to access the ReportPreviewModel. 
            public void CustomizeReportPreviewModel(DevExpress.Xpf.Printing.ReportPreviewModel model) {
                model.CustomizeParameterEditors += model_CustomizeParameterEditors;
            }
    
            List<CategoryInfo> categories;
    
            void model_CustomizeParameterEditors(object sender, CustomizeParameterEditorsEventArgs e) {
                if (e.Parameter.Name == "categories") {
                    ComboBoxEdit comboBox = new ComboBoxEdit();
                    comboBox.StyleSettings = new CheckedComboBoxStyleSettings();
                    comboBox.DisplayMember = "Name";
                    comboBox.ValueMember = "ID";
                    comboBox.ItemsSource = categories;
                    e.Editor = comboBox;
                    e.BoundDataMember = "EditValue";
                    e.BoundDataConverter = new CategoryIDsToStringConverter();
                }
            }
    
            partial void ReportPreviewScreen_Activated() {
                // Assign the name of the report that you want to preview in this screen. 
                this.ReportTypeName = "LightSwitchApplication.XtraReport1";
                categories = new List<CategoryInfo>();
                foreach (Category category in new DataWorkspace().NorthWindData.Categories) {
                    categories.Add(new CategoryInfo() { Name = category.CategoryName, ID = category.CategoryID });
                }
            }
        }
    }
    

Run the application and view the result.

LightSwitch - MultiValueParameters03

See Also