fastreport.net 使用table报错?

[图片] [图片] [图片] [图片] 按照网上的方法结果出现问题
关注者
5
被浏览
1,497

2 个回答

因为没有看到你的报表文件,不知道你的报表文件有没有问题。看报错应该绑定的数据源有问题?

我把几个文件的源文件给你,我在测试的时候报表中加多了一列数字。

下面是test.frx的源文件,新建文本文件,把代码复制进去,把扩展名改为frx

<?xml version="1.0" encoding="utf-8"?>
<Report ScriptLanguage="CSharp" ReportInfo.Created="01/31/2018 20:21:17" ReportInfo.Modified="01/31/2018 21:31:50" ReportInfo.CreatorVersion="2013.2.5.0">
  <ScriptText>using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using FastReport;
using FastReport.Data;
using FastReport.Dialog;
using FastReport.Barcode;
using FastReport.Table;
using FastReport.Utils;
namespace FastReport
  public class ReportScript
    private void Table1_ManualBuild(object sender, EventArgs e)
      DataSourceBase data1 = Report.GetDataSource("Table1"); // 获取DataSet中表名为Table1的数据源
      data1.Init(); // 初始化                                                             
      Table1.PrintRow(0); // 控件Table1打印第0行
      Table1.PrintColumns(); // 每打印一行,都要调用 PrintColumn或PrintColumns    
      while(data1.HasMoreRows) // 打印重复循环的行
        Table1.PrintRow(1);
        Table1.PrintColumns();
data1.Next(); // 读取下一行
</ScriptText>
  <Dictionary>
    <TableDataSource Name="Table1" ReferenceName="Data.Table1" DataType="System.Int32" Enabled="true">
      <Column Name="姓名" DataType="System.String" PropName="Column"/>
      <Column Name="密码" DataType="System.String" PropName="Column"/>
      <Column Name="数字" DataType="null" PropName="Column"/>
    </TableDataSource>
  </Dictionary>
  <ReportPage Name="Page1">
    <ReportTitleBand Name="ReportTitle1" Width="718.2" Height="37.8"/>
    <PageHeaderBand Name="PageHeader1" Top="41.8" Width="718.2" Height="28.35"/>
    <DataBand Name="Data1" Top="74.15" Width="718.2" Height="103.95">
      <TableObject Name="Table1" Left="75.6" Top="9.45" Width="340.2" Height="37.8" ManualBuildEvent="Table1_ManualBuild">
        <TableColumn Name="Column1" Width="119.7"/>
        <TableColumn Name="Column2" Width="110.25"/>
        <TableColumn Name="Column3" Width="110.25"/>
        <TableRow Name="Row1">
          <TableCell Name="Cell1" Text="姓名" Font="宋体, 9pt"/>
          <TableCell Name="Cell2" Text="密码" Font="宋体, 9pt"/>
          <TableCell Name="Cell8" Text="数字" Font="宋体, 9pt"/>
        </TableRow>
        <TableRow Name="Row2">
          <TableCell Name="Cell6" Text="[Table1.姓名]" Font="宋体, 9pt"/>
          <TableCell Name="Cell7" Text="[Table1.密码]" Font="宋体, 9pt"/>
          <TableCell Name="Cell9" Text="[Table1.数字]" Font="宋体, 9pt"/>
        </TableRow>
      </TableObject>
    </DataBand>
    <PageFooterBand Name="PageFooter1" Top="182.1" Width="718.2" Height="18.9"/>
  </ReportPage>
</Report>


c#中新建个窗体,放一个button,添加双击事件。

using FastReport;
using FastReport.Data;
using FastReport.Export.Pdf;
using FastReport.Export.Csv;
using FastReport.Export.OoXML;
using FastReport.Export.Xml;
using FastReport.Export;
using System;
using System.Data;
using System.Windows.Forms;
namespace TESTFP
    public partial class Form1 : Form
        public Form1()
            InitializeComponent();
        private void fpprint()
            DataSet ds = new DataSet();
            DataTable table1 = new DataTable();
            table1.TableName = "Table1"; // 一定要设置表名称
            ds.Tables.Add(table1);
            // 添加表中的列
            table1.Columns.Add("姓名", typeof(string));
            table1.Columns.Add("密码", typeof(string));
            table1.Columns.Add("数字", typeof(int));
            // 任意添加一些数据
            for (int i = 0, maxI = 10; i < maxI; i++)
                DataRow row = table1.NewRow();
                row["姓名"] = "我是" + i.ToString();
                row["密码"] = "0"+i.ToString();
                row["数字"] = i*10;
                table1.Rows.Add(row);
            Report FReport = new Report();
            string sPath = Application.StartupPath.ToString() + "\\test.frx";
            FReport.Load(sPath);
            // 将DataSet对象注册到FastReport控件中
            FReport.RegisterData(ds);
            FReport.Prepare();
            //报表导出为PDF
            PDFExport export = new PDFExport();
           // FastReport.Export.
            FReport.Export(export,  DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf");
            CSVExport csvport = new CSVExport();
            FReport.Export(csvport, DateTime.Now.ToString("yyyyMMddHHmmss") + ".csv");
            Excel2007Export xlmsexport = new Excel2007Export();
            FReport.Export(xlmsexport, DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx");
            XMLExport xlsexport = new XMLExport();
            FReport.Export(xlsexport, DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
FReport.Show();
            // 释放资源  
            FReport.Dispose();
        private void button1_Click(object sender, EventArgs e)
            fpprint();