XtraReportBase.DataSourceDemanded Event
Occurs before report generation, to specify a data source for the report.
Namespace: DevExpress.XtraReports.UI
Assembly: DevExpress.XtraReports.v17.2.dll
NuGet Package: DevExpress.Reporting.Core
Declaration
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.
At present, query parameters cannot be directly mapped to multi-value report parameters, and this task is solved by adding a WHERE clause to the SQL string in the following way.
SELECT [Categories].[CategoryID], [Categories].[CategoryName] FROM [Categories] [Categories]
WHERE [Categories].[CategoryID] IN (1,2,3)
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.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/how-to-pass-a-multi-value-parameter-to-a-query-t352379.
Imports DevExpress.DataAccess.Sql
Imports System
Imports System.Collections
Imports System.Text
' ...
Namespace WindowsFormsApplication3
Partial Public Class XtraReport1
Inherits DevExpress.XtraReports.UI.XtraReport
Public Sub New()
InitializeComponent()
' 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 CustomSqlQuery = TryCast(Me.sqlDataSource1.Queries(0), CustomSqlQuery)
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.Sql &= " WHERE [Categories].[CategoryID] IN " & builder.ToString()
sqlDataSource1.RebuildResultSchema()
End Sub
End Class
End Namespace