如何在报表中使用表单控件属性?
How to use form controls properties in a report?
我正在尝试为学校管理系统开发一个简单的数据库前端,我正在使用 ReportViewer 来显示报告中的一些数据,但是我想在报告中显示一些数据,而不是在数据库中,但在 UI 中作为用户输入(想想 myTextBox.Text)!如何才能做到这一点 ?
哦,我的 ReportViewer 与我想使用的用户输入字段采用不同的表单,让两个表单一起通信没有问题,但是如何从其中一个获取数据表格(控件属性)到报表中!
我正在使用 Visual Studio 2013,C#,数据库是 MS Access .accdb。
谢谢。
如果我理解你的问题是正确的:你首先将用户带到一个带有一些输入控件的页面。当 s/he 输入一个值并提交表单时,您将用户重定向到一个报告页面,该页面将显示用户输入的数据以及从数据库中提取的其他数据。如果这是真的,这对你有帮助:
You can pass information between pages in various ways, some of which
depend on how the redirection occurs. The following options are
available even if the source page is in a different ASP.NET Web
application from the target page, or if the source page is not an
ASP.NET Web Forms page:
- Use a query string.
Get HTTP POST information from the source page.
The following options are available only when the source and target pages are in the same ASP.NET Web application.
- Use session state.
- Create public properties in the source page and access the property values in the target page.
- Get control information in the target page from controls in the source page.
来源:https://msdn.microsoft.com/en-us/library/6c3yckfw.aspx
编辑:如果处理 windows 表单,以下内容可能会有所帮助。我通常不使用 WinForms,因此请查看源代码 link 了解更多详情:
在接收表单上创建一个方法来处理接收到的数据,像这样:
internal void LoadCustomData(CustomClass customObject)
{
// Update the local DataSet with the data in customObject
}
在输入表单上调用该方法,在接收表单上调用Show()方法,如下所示:
private void customersDataGridView_DoubleClick(object sender, EventArgs e)
{
// Get data from the input control
CustomObject data;
// Pass the data to receiving form
ReceivingForm reportForm = new ReceivingForm();
reportForm.LoadCustomData(data);
reportForm.Show();
}
我想我找到了解决办法!
这只是使用报告参数的简单问题。应按如下方式使用:
假设我的主窗体 (MyForm) 包含一个文本框 (myTextBox),我想将其值打印在报表(myReport.rdlc,客户端报表)中的某处,该报表显示在 ReportViewer 组件上(我的报告查看器)。
我要做的第一件事是向 myReport 添加一个报表参数。在报表设计器中,在“报表数据”选项卡(最左侧的面板)中,我单击“新建”>“参数”,然后填写参数的详细信息(名称、类型、允许空值、默认值...等),假设我将其命名为 paramText , 类型 = Text.
然后在报表设计界面中,我们将保存数据的字段的表达式设置如下:
=Parameters!paramText.Value
现在,回到我们的 C# 代码中,我刚刚将以下代码添加到我的 myFome_Load 事件中以将 myTextBox 中的文本与 paramText 相关联:
myReportViewer.LocalReport.SetParameters(new ReportParameter("paramText", myTextBox.Text));
就是这样,这样出现在 myTextBox 中的文本将显示在您在 myReport 中定义的某个位置!这可以对任何 WinForm 控件或 属性 完成,只要您在设置参数时尊重 属性 的输入即可。
如果ReportViewer在另一个Form中,例如我的情况,我只是暴露了myTextBox(访问方法)的Text属性,并在另一个窗体中使用它。
PS。确保包括:
using Microsoft.Reporting.WinForms;
我正在尝试为学校管理系统开发一个简单的数据库前端,我正在使用 ReportViewer 来显示报告中的一些数据,但是我想在报告中显示一些数据,而不是在数据库中,但在 UI 中作为用户输入(想想 myTextBox.Text)!如何才能做到这一点 ?
哦,我的 ReportViewer 与我想使用的用户输入字段采用不同的表单,让两个表单一起通信没有问题,但是如何从其中一个获取数据表格(控件属性)到报表中!
我正在使用 Visual Studio 2013,C#,数据库是 MS Access .accdb。
谢谢。
如果我理解你的问题是正确的:你首先将用户带到一个带有一些输入控件的页面。当 s/he 输入一个值并提交表单时,您将用户重定向到一个报告页面,该页面将显示用户输入的数据以及从数据库中提取的其他数据。如果这是真的,这对你有帮助:
You can pass information between pages in various ways, some of which depend on how the redirection occurs. The following options are available even if the source page is in a different ASP.NET Web application from the target page, or if the source page is not an ASP.NET Web Forms page:
- Use a query string.
Get HTTP POST information from the source page.
The following options are available only when the source and target pages are in the same ASP.NET Web application.
- Use session state.
- Create public properties in the source page and access the property values in the target page.
- Get control information in the target page from controls in the source page.
来源:https://msdn.microsoft.com/en-us/library/6c3yckfw.aspx
编辑:如果处理 windows 表单,以下内容可能会有所帮助。我通常不使用 WinForms,因此请查看源代码 link 了解更多详情:
在接收表单上创建一个方法来处理接收到的数据,像这样:
internal void LoadCustomData(CustomClass customObject)
{
// Update the local DataSet with the data in customObject
}
在输入表单上调用该方法,在接收表单上调用Show()方法,如下所示:
private void customersDataGridView_DoubleClick(object sender, EventArgs e)
{
// Get data from the input control
CustomObject data;
// Pass the data to receiving form
ReceivingForm reportForm = new ReceivingForm();
reportForm.LoadCustomData(data);
reportForm.Show();
}
我想我找到了解决办法! 这只是使用报告参数的简单问题。应按如下方式使用:
假设我的主窗体 (MyForm) 包含一个文本框 (myTextBox),我想将其值打印在报表(myReport.rdlc,客户端报表)中的某处,该报表显示在 ReportViewer 组件上(我的报告查看器)。
我要做的第一件事是向 myReport 添加一个报表参数。在报表设计器中,在“报表数据”选项卡(最左侧的面板)中,我单击“新建”>“参数”,然后填写参数的详细信息(名称、类型、允许空值、默认值...等),假设我将其命名为 paramText , 类型 = Text.
然后在报表设计界面中,我们将保存数据的字段的表达式设置如下:
=Parameters!paramText.Value
现在,回到我们的 C# 代码中,我刚刚将以下代码添加到我的 myFome_Load 事件中以将 myTextBox 中的文本与 paramText 相关联:
myReportViewer.LocalReport.SetParameters(new ReportParameter("paramText", myTextBox.Text));
就是这样,这样出现在 myTextBox 中的文本将显示在您在 myReport 中定义的某个位置!这可以对任何 WinForm 控件或 属性 完成,只要您在设置参数时尊重 属性 的输入即可。
如果ReportViewer在另一个Form中,例如我的情况,我只是暴露了myTextBox(访问方法)的Text属性,并在另一个窗体中使用它。
PS。确保包括:
using Microsoft.Reporting.WinForms;