Back to Devexpress

XtraReportBase.DataSourceDemanded Event

xtrareports-devexpress-dot-xtrareports-dot-ui-dot-xtrareportbase-2af51229.md

latest6.1 KB
Original Source

XtraReportBase.DataSourceDemanded Event

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

Namespace : DevExpress.XtraReports.UI

Assembly : DevExpress.XtraReports.v25.2.dll

NuGet Package : DevExpress.Reporting.Core

Declaration

csharp
public event EventHandler<EventArgs> DataSourceDemanded
vb
Public Event DataSourceDemanded As EventHandler(Of EventArgs)

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.

csharp
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();
        }
    }
vb
Imports DevExpress.DataAccess.Sql
Imports System
Imports System.Collections
Imports System.Text
' ...

Partial Public Class XtraReport1
    Inherits DevExpress.XtraReports.UI.XtraReport

    Public Sub New()
        InitializeComponent()

        Dim query As SelectQuery = SelectQueryFluentBuilder
        .AddTable("Categories")
        .SelectAllColumns()
        .Build("Categories")
        Me.sqlDataSource1.Queries.Clear()
        Me.sqlDataSource1.Queries.Add(query)

        ' Assign a set of values to the report parameter.
        Me.parameter1.Value = New Integer() { 1, 2, 3 }

        ' Handle the DataSourceDemanded event of a report.
        AddHandler Me.DataSourceDemanded, AddressOf XtraReport1_DataSourceDemanded
    End Sub

    Private Sub XtraReport1_DataSourceDemanded(ByVal sender As Object, ByVal e As EventArgs)
        Dim query As SelectQuery = TryCast(Me.sqlDataSource1.Queries("Categories"), SelectQuery)
        Dim count As Integer = (TryCast(Me.parameter1.Value, IList)).Count
        If count = 0 Then
            Return
        End If
        Dim builder As New StringBuilder()
        builder.Append("("c)
        For i As Integer = 0 To count - 1
            'builder.Append('\''); // Uncomment this line when parsing a string parameter value.
            builder.Append((TryCast(Me.parameter1.Value, IList))(i))
            'builder.Append('\''); // Uncomment this line when parsing a string parameter value.
            If i <> count - 1 Then
                builder.Append(","c)
            End If
        Next i

        builder.Append(")"c)
        query.FilterString = "[Categories].[CategoryID] IN " & builder.ToString()
        sqlDataSource1.RebuildResultSchema()
    End Sub
End Class

See Also

Use Query Parameters

Report Events

XtraReportBase Class

XtraReportBase Members

DevExpress.XtraReports.UI Namespace