Skip to main content

XtraReportBase.DataSourceDemanded Event

Occurs before report generation, to specify a data source for the report.

Namespace: DevExpress.XtraReports.UI

Assembly: DevExpress.XtraReports.v24.1.dll

NuGet Package: DevExpress.Reporting.Core

Declaration

public event EventHandler<EventArgs> DataSourceDemanded

Event Data

The DataSourceDemanded event's data class is EventArgs.

Remarks

In a handler of this event, you can set or modify a value of the report’s XtraReportBase.DataSource property.

You can handle the DataSourceDemanded event to silently pass parameters to a report and populate the report’s data source using a stored procedure.

When this event is raised, all report parameters have already been initialized or assigned values in a Print Preview. You can handle this event to obtain their values.

Example

This example illustrates a workaround enabling you to use multi-value parameters in a query string.

For more information, review the following help topic: Bind a Multi-Value Report Parameter to a Query Parameter.

To dynamically update the query string using the values assigned to a report parameter (whose Parameter.MultiValue property is set to true), use the following code in the XtraReportBase.DataSourceDemanded event handler.

using DevExpress.DataAccess.Sql;
using System;
using System.Collections;
using System.Text;
// ...


    public partial class XtraReport1 : DevExpress.XtraReports.UI.XtraReport {
        public XtraReport1() {
            InitializeComponent();

            SelectQuery query = SelectQueryFluentBuilder
                .AddTable("Categories")
                .SelectAllColumns()
                .Build("Categories");
            this.sqlDataSource1.Queries.Clear();
            this.sqlDataSource1.Queries.Add(query);

            // Assign a set of values to the report parameter.
            this.parameter1.Value = new int[] { 1, 2, 3 };

            // Handle the DataSourceDemanded event of a report.
            this.DataSourceDemanded += XtraReport1_DataSourceDemanded;
        }

        void XtraReport1_DataSourceDemanded(object sender, EventArgs e) {
            SelectQuery query = this.sqlDataSource1.Queries["Categories"] as SelectQuery;
            int count = (this.parameter1.Value as IList).Count;
            if (count == 0)
                return;
            StringBuilder builder = new StringBuilder();
            builder.Append('(');
            for (int i = 0; i < count; i++) {
                //builder.Append('\''); // Uncomment this line when parsing a string parameter value.
                builder.Append((this.parameter1.Value as IList)[i]);
                //builder.Append('\''); // Uncomment this line when parsing a string parameter value.
                if (i != count - 1)
                    builder.Append(',');
            }

            builder.Append(')');
            query.FilterString = "[Categories].[CategoryID] IN " + builder.ToString();
            sqlDataSource1.RebuildResultSchema();
        }
    }
See Also