Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

c# - 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();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...