在WinForm项目中使用FastReport.Net(二、使用控件搭建窗体)

上一节我们新建了工程、引入了控件并且写好了启动页的逻辑,现在我们来真正的引入控件

准备一个FastReport报表

  • 使用安装时我们的设计工具设计一张最简单的报表


    菜单

    设计的报表,只有一个文字框
  • 将这份报表保存到工程文件/bin/Debug/Report下


    保存的地方

引入Preview控件

  • 我们在PreviewForm中,将PreviewControl控件拖入窗体,将窗体拉大一点,然后将控件的Dock设为Fill


    拖进去

    Dock

    效果
  • 然后我们F5测试一下看看是什么效果


    我们发现控件被正确的显示出来了
  • 那怎么才能看到我们报表呢,我们需要用代码来加载,我们双击Form,新建Load函数,打下面的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FastReport; //引入FastReport
namespace ReportDemo
{
    public partial class PreviewForm : Form
    {
        private Report pReport; //新建一个私有变量
        public PreviewForm()
        {
            InitializeComponent();
        }

        private void PreviewForm_Load(object sender, EventArgs e)
        {
            pReport = new Report();   //实例化一个Report报表
            String reportFile = "Report/report.frx";
            pReport.Load(reportFile);  //载入报表文件
            pReport.Preview = previewControl1; //设置报表的Preview控件(这里的previewControl1就是我们之前拖进去的那个)
            pReport.Prepare();   //准备
            pReport.ShowPrepared();  //显示
        }
    }
}

  • 我们再F5一下


    载入了报表文件的样子
  • 这这里我们已经可以预览我们的报表了 但是在我们的需求中,用户还需要自定义报表的内容和格式呢,我们下一步就在实现报表设计器

引入Design控件

  • 我们像Preview那样把Design控件拖进DesignForm,然后Dock设为Fill


    设计
  • 然后我们来写怎么样吧设计器绑定Report文件,双击新建Load函数,引入FastReport,新建一个private变量

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FastReport; //引入FastReport
namespace ReportDemo
{
    public partial class DesignForm : Form
    {
        private Report dReport;
        public DesignForm()
        {
            InitializeComponent();
        }

        private void DesignForm_Load(object sender, EventArgs e)
        {
            dReport = new Report();
            String reportFile = "Report/report.frx";
            dReport.Load(reportFile);
            this.designerControl1.Report = dReport; //这里不一样的是把Report赋给控件的属性
            dReport.Prepare();
            dReport.Design(); 
        }
    }
}

  • 我们F5一下


    成功!

至此我们已经可以浏览和设计我们的报表了,但是我们还没有数据,下一节我们就就来连接一下数据库,看看FastReport是怎么传递数据的吧!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容