如何从组合框将参数传递给devexpress XtraReport
How to pass parameters to devexpress XtraReport from combobox
我有一个包含 3 个组合框和一个按钮的表单,如下所示
以及包含 3 个绑定到富文本的参数的报告
我在单击“打印”按钮时为此过程使用了以下代码,但未传递参数且富文本字段为空
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();
}
使用 XtraReport.Parameters
集合将组合框值传递到参数名称中,如下例所示:
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();
}
或者您可以声明一个重载构造函数,它在其中分配参数值,然后使用重载构造函数参数创建 XtraReport
实例:
// 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();
}
参考:Passing Parameter Values at Runtime
更新:
如果每个文本框的 SelectedText
属性 总是有空值,您可以使用 Text
或 SelectedItem
属性 来获取实际的组合框值 (类似问题 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();
}
我有一个包含 3 个组合框和一个按钮的表单,如下所示
以及包含 3 个绑定到富文本的参数的报告
我在单击“打印”按钮时为此过程使用了以下代码,但未传递参数且富文本字段为空
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();
}
使用 XtraReport.Parameters
集合将组合框值传递到参数名称中,如下例所示:
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();
}
或者您可以声明一个重载构造函数,它在其中分配参数值,然后使用重载构造函数参数创建 XtraReport
实例:
// 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();
}
参考:Passing Parameter Values at Runtime
更新:
如果每个文本框的 SelectedText
属性 总是有空值,您可以使用 Text
或 SelectedItem
属性 来获取实际的组合框值 (类似问题 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();
}