报表控件FastReport.NET使用教程:将初步数据输入表格的交互式报告

发布时间 : 2022-06-29 11:40:52.513|阅读 249 次

概述:FastReport.NET 报表可以具有在生成报表之前显示的对话框表单。它们允许您设置一些文本、布尔值或数字变量。

相关链接:

FastReport.NET 报表可以具有在生成报表之前显示的对话框表单。它们允许您设置一些文本、布尔值或数字变量。这些可以是文本字段、表格、列表、下拉列表、日期、复选框,甚至是带有复选框的列表。

通常,对话框表单用于过滤报表中的数据或选择报表行为的标准。但是今天我们将讨论对话形式的另一种可能用途。

让我们看一下需要在显示之前为报表输入数据的情况。当涉及到单个文本字段、复选框、列表时,这是一个简单的案例。

但是,如果您有一个数据表,并且想要在构建报告之前手动调整它怎么办?

这是 Grid 网格 组件可以提供帮助的地方。这是一个包含数据的表格,我们可以在构建报告之前在对话框中显示这些数据。

让我们在报表中添加一个对话框窗体并在其上放置一个 Grid 控件。让我们通过右键单击来调用它的上下文菜单:

FastReport.NET官方版下载

FastReport.NET

选择“编辑列...”以将列添加到表中。

FastReport.NET

在列编辑窗口中添加三列 - 姓名、地址、电话。查看Customers.Name 属性。在这里,我们引用了尚未在报告中的客户数据源。但我们稍后会使用脚本添加它。对于其余列,您需要设置适当的属性。

报告页面模板非常简单——只有一个三列的 Table 对象。我们将把它填充到报告脚本中。

FastReport.NET

现在,让我们为报表添加 StartReport 事件处理程序:

FastReport.NET

报告脚本:

public class ReportScript
 {
 //Data structure for table
 public class Customer 
 {
 public string Name {get; set; }
 public string Address {get; set; }
 public string Phone {get; set; }
 
 public Customer(string name, string address, string phone)
 {
 Name = name;
 Address = address;
 Phone = phone;
 }
 }
 
 private void _StartReport(object sender, EventArgs e)
 {
//List of customers
Listcustomers = new List();
//Fill the list of customers with default data
 customers.Add(new Customer("Kevin Smith", "221 52nd st, Brooklyn, NY, United States", "+12127599755"));
 customers.Add(new Customer("Justin Ford", "1556 Broadway, suite 416, NY, United States", "+12145678900"));
 customers.Add(new Customer("Amanda Stephenson", "455 Larkspur Dr., CA, United States", "+14105175379"));
// Register the data source in a report
 Report.RegisterData(customers, "Customers");
//Set the data source in the table
 Grid1.DataSource = Report.GetDataSource("Customers");
//Set fields in cells
 Cell6.Text = "[Customers.Name]";
 Cell7.Text = "[Customers.Address]";
 Cell8.Text = "[Customers.Phone]";
 }
}

在这种情况下,我们设置了将用于在对话框和报表中显示表中的行的 Customer 数据结构。接下来,我们创建一个客户数据源并使用客户实例填充它。然后我们在报表中注册接收到的数据源。你还记得我们是如何为 Grid 对象中的列设置数据字段的吗?我们提到了这个数据源。在这里,我们将源中的字段分配给页面模板中的表格单元格(表格对象)。

现在让我们为报表页面上的 Table 对象创建一个 ManualBuild 事件处理程序。在页面上构建对象后调用此事件,并允许您更改准备显示的表格。因此,我们可以使用脚本更改表格中的文本。

FastReport.NET

 private void Table1_ManualBuild(object sender, EventArgs e)
 {
 //Set the data source
 DataSourceBase rowData = Report.GetDataSource("Customers");
 //Initialize the data 
 rowData.Init();
 //Display the first row of data
 Table1.PrintRow(0);
 //Display the column
 Table1.PrintColumns();
 //Loop through all rows of data in source
 while (rowData.HasMoreRows)
 {
 //Output the next line of data
 Table1.PrintRow(1);
 //Output the column
 Table1.PrintColumns();
 //take the following entry from the source
 rowData.Next();
 }
 }

在这里,我们通过简单地遍历所有数据行来填充表格。

让我们运行报告。我们将看到的第一件事是一个带有表格的对话框:

FastReport.NET

让我们双击第一个单元格来编辑它的文本:

FastReport.NET

报表控件 FastReport.NET 单击确定并检查结果:

FastReport.NET

如您所见,我们的默认数据已更改为我们手动输入的数据。通过这种方式,您可以让用户在构建报表之前手动更改数据。此示例显示如何使用来自脚本的数据填充表,但没有什么能阻止您使用来自源的数据填充它。

更多产品授权信息点击查看FastReport.NET价格,或者咨询慧都在线客服。

FastReport.NET | 在线试用


FastReport.NET技术QQ群:702295239      欢迎进群一起讨论


在线
客服
微信
QQ 电话
023-68661681
返回
顶部