FastReport studio 动态加载数据集 (zhuan)

 

测试使用的FastReport Studio 版本为4.6.80

先引用FastReport.dll

然后把C:/Program Files/FastReports/FastReport Studio Trial/Examples/VisualC#.NET/DataSetDemo目录下的

FrxDataSet.cs

FrxDataTable.cs

FrxDataTable.resx

复制到解决方案中(切记要将这三个文件代码中的命名空间更改为自己程序的命名)

SQLServer数据库,表名demo_test  有三个字段 id(int),name(char),onemorename(char)

FastReport报表demo_test.fr3中有三个memo

[demo_test."id"]        DataField属性 id

[demo_test."name"]        DataField属性 name

[demo_test."onemorename"]        DataField 属性 onemorename

以下是我根据DataSetDemo写的代码,在VS2005中测试通过

 view plaincopy to clipboardprint?
using System;   
using System.Collections.Generic;   
using System.ComponentModel;   
using System.Data;   
using System.Drawing;   
using System.Text;   
using System.Windows.Forms;   
using FastReport;   
using System.Data.SqlClient;   
  
namespace FRDemoTest   
{   
    public partial class Form1 : Form   
    {   
        TfrxReportClass report;   
        FrxDataTable datatable;   
        FrxDataSet dataset;   
  
        public Form1()   
        {   
            InitializeComponent();   
        }   
    //为FrxDataTable加载数据   
        private void FillTableWithSampleData(FrxDataTable datatable)   
        {   
            SqlConnection conn = new SqlConnection("server=.;database=DEMO;UID=sa;PWD=");   
            SqlDataAdapter da = new SqlDataAdapter("select * from demo_test", conn);   
            da.Fill(datatable);   
        }   
    //为FrxDataSet加载数据   
        private void FillDataSetWithSampleData(FrxDataSet dataset)   
        {   
            SqlConnection conn = new SqlConnection("server=.;database=DEMO;UID=sa;PWD=");   
            SqlDataAdapter da = new SqlDataAdapter("select top 2 * from demo_test", conn);   
            da.Fill(dataset,"demo_test");//demo_test为表名,对应FastReport模板中Memo的名字[demo_test."id"]   
        }   
    //加载FrxDataTable   
        private void button1_Click(object sender, EventArgs e)   
        {   
            report = new TfrxReportClass();   
            report.ClearDatasets();//加这句可以避免重复打开报表提示错误   
            datatable = new FrxDataTable("demo_test");//demo_test为表名,对应FastReport模板中Memo的名字[demo_test."id"]   
            FillTableWithSampleData(datatable);   
            //report.MainWindowHandle = (int)this.Handle;   
            report.LoadReportFromFile("demo_test.fr3");   
            //report.ClearDatasets();   
            datatable.AssignToReport(true, report);   
            datatable.AssignToDataBand("MasterData1", report);   
            report.ShowReport();   
        }   
    //加载FrxDataSet   
        private void button2_Click(object sender, EventArgs e)   
        {   
            report = new TfrxReportClass();   
            report.ClearDatasets(); //加这句可以避免重复打开报表提示错误   
            dataset = new FrxDataSet();   
            FillDataSetWithSampleData(dataset);   
            report.LoadReportFromFile("demo_test.fr3");   
            dataset.BindToReport(report);   
            dataset.BindTableToBand("demo_test", report, "MasterData1");   
            report.ShowReport();   
        }   
    //加载Dedigner   
        private void button3_Click(object sender, EventArgs e)   
        {   
            report = new TfrxReportClass();   
            report.LoadReportFromFile("demo_test.fr3");   
            report.DesignReport();   
        }   
    }   
}  
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using FastReport;
using System.Data.SqlClient;

namespace FRDemoTest
{
    public partial class Form1 : Form
    {
        TfrxReportClass report;
        FrxDataTable datatable;
        FrxDataSet dataset;

        public Form1()
        {
            InitializeComponent();
        }
 //为FrxDataTable加载数据
        private void FillTableWithSampleData(FrxDataTable datatable)
        {
            SqlConnection conn = new SqlConnection("server=.;database=DEMO;UID=sa;PWD=");
            SqlDataAdapter da = new SqlDataAdapter("select * from demo_test", conn);
            da.Fill(datatable);
        }
 //为FrxDataSet加载数据
        private void FillDataSetWithSampleData(FrxDataSet dataset)
        {
            SqlConnection conn = new SqlConnection("server=.;database=DEMO;UID=sa;PWD=");
            SqlDataAdapter da = new SqlDataAdapter("select top 2 * from demo_test", conn);
            da.Fill(dataset,"demo_test");//demo_test为表名,对应FastReport模板中Memo的名字[demo_test."id"]
        }
 //加载FrxDataTable
        private void button1_Click(object sender, EventArgs e)
        {
            report = new TfrxReportClass();
            report.ClearDatasets();//加这句可以避免重复打开报表提示错误
            datatable = new FrxDataTable("demo_test");//demo_test为表名,对应FastReport模板中Memo的名字[demo_test."id"]
            FillTableWithSampleData(datatable);
            //report.MainWindowHandle = (int)this.Handle;
            report.LoadReportFromFile("demo_test.fr3");
            //report.ClearDatasets();
            datatable.AssignToReport(true, report);
            datatable.AssignToDataBand("MasterData1", report);
            report.ShowReport();
        }
 //加载FrxDataSet
        private void button2_Click(object sender, EventArgs e)
        {
            report = new TfrxReportClass();
            report.ClearDatasets(); //加这句可以避免重复打开报表提示错误
            dataset = new FrxDataSet();
            FillDataSetWithSampleData(dataset);
            report.LoadReportFromFile("demo_test.fr3");
            dataset.BindToReport(report);
            dataset.BindTableToBand("demo_test", report, "MasterData1");
            report.ShowReport();
        }
 //加载Dedigner
        private void button3_Click(object sender, EventArgs e)
        {
            report = new TfrxReportClass();
            report.LoadReportFromFile("demo_test.fr3");
            report.DesignReport();
        }
    }
}

你可能感兴趣的:(port)