Search code examples
winformstelerikreportreportingtelerik-reporting

Telerik Reports parameters from WindowsForm Codebehind to Report


I'm working on a Windows Forms Application where I want to load Reports into a Reportviewer after a click on a Button. This is the Event that gets triggered by pressing on the button in the Code behind of the Windows Form:

    private void button1_Click(object sender, EventArgs e)
{

    Telerik.Reporting.InstanceReportSource reportSource = new
    Telerik.Reporting.InstanceReportSource();
    reportSource.ReportDocument = new Reportlibrary.Report1();

    reportSource.Parameters.Add(new Telerik.Reporting.Parameter("OrderNumber","123456789"));

    reportViewer1.ReportSource = reportSource;
    reportViewer1.RefreshReport();

}

The problem now is that I have no Idea how I can access / get the parameter I added before Refreshing the Reportviewer. The Report already has set a Datasource. I don't know if this matters. This is what I have right now. I've tried everything and I'm just not getting further.

        public Report1()
        {
            InitializeComponent();

            Position[] all = new Position[]{

               new Position("Test", "Test","test"),

            };

            this.DataSource = all;

             MessageBox.Show("Number: " +
             this.Report.ReportParameters["OrderNumber"].Value.ToString());

        }

Is there any way to get this parameter straight after InitializeComponent(); ? Do I need to add another Event to the report to access it? If yes which on is the best way to do this?

Any help very apreciated. Thank you


Solution

  • Set the parameters of the report on an instance of the report itself (not the report source), such as:

            TopPageViews report = new TopPageViews();
            report.ReportParameters["StartDate"].Value = new DateTime(2013, 3, 1);
            report.ReportParameters["EndDate"].Value = new DateTime(2013, 3, 1);
    
            InstanceReportSource reportSource = new InstanceReportSource();
            reportSource.ReportDocument = report;
    
            this.reportViewer1.ReportSource = reportSource;
            this.reportViewer1.RefreshReport();
    

    In your report constructor, after InitializeComponent, subscribe a handler to the ItemDataBinding event:

        this.ItemDataBinding += TopPageViews_ItemDataBinding;
    

    And in your handler, you can obtain the value as you normally would:

        DateTime startDateParm = (DateTime)this.ReportParameters["StartDate"].Value;
    

    You can use the debugger to see the value.