Example T432556
Visible to All Users

Reporting for WPF - How to Customize a Report Data Source in Code

This example illustrates how to replace an existing component (data source) with a new one and assign it to a report.

End-User Report Designer - Change data source

Files to Review

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

MainWindow.xaml.cs(vb)
C#
using System.ComponentModel; using System.Windows; using DevExpress.DataAccess.ObjectBinding; using DevExpress.Xpf.Reports.UserDesigner; using DevExpress.XtraReports.UI; namespace WpfApplication1 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); designer.OpenDocument(new XtraReport1()); } void ReplaceDataSource_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e) { var newDataSource = new ObjectDataSource { DataSource = typeof(MyDataClass) }; XtraReport report = designer.ActiveDocument.Diagram.RootItem.XRObject; var oldDataSource = report.DataSource as IComponent; designer.ActiveDocument.MakeChanges(changes => { if(oldDataSource != null) { changes.RemoveItem(oldDataSource); } changes.AddItem(newDataSource); changes.SetProperty(report, x => x.DataSource, newDataSource); }); MessageBox.Show("The data source was replaced from SQL to Object.\n\n Click Undo to cancel."); } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.