How to pass parameters to devexpress XtraReport from combobox

让人想犯罪 __ 提交于 2019-12-11 15:42:00

问题


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

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

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();
}

回答1:


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();
}


来源:https://stackoverflow.com/questions/51570284/how-to-pass-parameters-to-devexpress-xtrareport-from-combobox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!