Search code examples
c#devexpressreportxtrareport

How to pass parameters to devexpress XtraReport from combobox


I have a form that contains 3 comboboxes and a button like shown below

enter image description here

and a report that containes 3 parameters that are bounded to richtext

enter image description here

i used the following code for this process when clicking the button Print but parameters aren't passed and richtext fields are empty

private void btnPrint_Click(object sender, EventArgs e)
{
    // Create a report instance.
    var report = new XtraReport1();

    // Obtain a parameter, and set its value.
    report.ClassName.Value    = cmbClass.SelectedText;
    report.SubclassName.Value = cmbDivision.SelectedText;
    report.StudentName.Value  = cmbStudent.SelectedText;

    report.RequestParameters = false;    // Hide the Parameters UI from end-users.
    report.ShowPreview();
}

Solution

  • Use XtraReport.Parameters collection to pass combobox values into parameter names as shown in below example:

    private void btnPrint_Click(object sender, EventArgs e)
    {
        // Create a report instance.
        var report = new XtraReport1();
    
        // Obtain a parameter, and set its value.
        report.Parameters["ClassName"].Value = cmbClass.SelectedText;
        report.Parameters["SubclassName"].Value = cmbDivision.SelectedText;
        report.Parameters["StudentName"].Value = cmbStudent.SelectedText;
    
        report.RequestParameters = false; // Hide the Parameters UI from end-users.
        report.ShowPreview();
    }
    

    Or you may declare an overload constructor which assigns parameter values inside it, then create XtraReport instance using overloaded constructor arguments:

    // XtraReport
    public partial class ReportName : DevExpress.XtraReports.UI.XtraReport
    {
        // default parameterless constructor here
    
        public ReportName(string ClassName, string SubclassName, string StudentName)
        {
            InitializeComponent();
    
            this.Parameters["ClassName"].Value = ClassName;
            this.Parameters["SubclassName"].Value = SubclassName;
            this.Parameters["StudentName"].Value = StudentName;
        }
    }
    
    // Form code
    private void btnPrint_Click(object sender, EventArgs e)
    {
        // Create a report instance.
        var report = new XtraReport1(cmbClass.SelectedText, cmbDivision.SelectedText, cmbStudent.SelectedText);
    
        report.RequestParameters = false; // Hide the Parameters UI from end-users.
        report.ShowPreview();
    }
    

    Reference: Passing Parameter Values at Runtime

    Update:

    If SelectedText property for each textbox always have null value, you can use Text or SelectedItem property to get actual combo box value (similar issue here).

    private void btnPrint_Click(object sender, EventArgs e)
    {
        // Create a report instance.
        var report = new XtraReport1();
    
        // Obtain a parameter, and set its value.
        report.Parameters["ClassName"].Value = cmbClass.Text;
        report.Parameters["SubclassName"].Value = cmbDivision.Text;
        report.Parameters["StudentName"].Value = cmbStudent.Text;
    
        report.RequestParameters = false; // Hide the Parameters UI from end-users.
        report.ShowPreview();
    }