您的位置:首页 > 其它

FastReport for .net 学习笔记

2013-05-09 14:53 344 查看
给报表赋数据源的两种方式

(一) 通过程序给报表注册数据源。

这种方式中,模板中不能有数据源,否则给报表注册数据源时会出错

这种方式中,用户不方便建模板,且不容易更改模板

这种方式中,程序可以很灵活的对数据源进行处理,然后再把处理过的数据源注册给报表

//获取数据集

string strConn = "data source=(Local);initial catalog=Hotel;user id=sa;pwd=;connect timeout=120";

string strSql = "select a.Rno,cast(a.BuildID as varchar(2))+'-'+c.BuildName as BuildName,a.Floorid+'-'+ b.FloorName as FloorName,a.Rtp+'-'+ d.cnm as RtpNam,a.RRT"+

" from rmst as a left join [floor] as b on a.floorid=b.floorid left join build as c on a.buildid=c.buildid left join stt as d on a.rtp=d.rtp";

SqlDataAdapter sda = new SqlDataAdapter(strSql, strConn);

DataSet ds = new DataSet();

sda.Fill(ds, "RoomInfo");

//加载报表

Report report = new Report();

report.Load(Application.StartupPath + "\\Report\\rptRoom.frx");

//给报表注册数据

report.RegisterData(ds.Tables["RoomInfo"], "RoomInfo");

//绑定数据到数据区域

DataBand band = report.FindObject("Data1") as DataBand;

band.DataSource = report.GetDataSource("RoomInfo");

this.previewControl1.BringToFront();

report.Preview = this.previewControl1;

if (report.Prepare())

report.ShowPrepared();

FastReport传送连接串的三种方式

(二) 建模板时,建立好数据源(可通过SQL语句等),然后在程序中给模板传递参数

这种方式中,用户建模板,修改模板简单。但因为数据源是定的,因此不是特别灵活。(比如在一个已查询出的记录集中,想手工再往里面添加点东西就办不到)

给模板传递连接参数有以下三种方式

1.直接传送一个连接字符给报表的连接对象( 加载之后,运行之前)

report1.Load(...);

report1.Dictionary.Connections[0].ConnectionString = my_connection_string;

report1.Show();

2.通过报表参数传递

2.1 运行报表设计器

2.2 在数据窗口中,创建一个新的报表参数 如 ConnStr

2.3 在数据窗口中,选择连接对象,然后切换到属性窗口,设置 ConnectionStringExpression 属性中,选择刚才建设好的参数

2.4 在程序中给参数赋值

Report report = new Report();

report.Preview = this.previewControl1;

report.Load(Application.StartupPath + "\\Report\\rptRoom2.frx");

report.SetParameterValue("ConnStr", textBox1.Text.Trim()); //通过参数给报表的连接串赋值

this.previewControl1.BringToFront();

if (report.Prepare())

{

report.ShowPrepared();

}

3.利用EnvironmentSettings控件的DatabaseLogin事件(此事件每当报表连接数据库时都会发生)

3.1 在窗体中拖入一个EnvironmnetSettings控件

3.2 此事件是全局的,对所有模块内的报表都有效

3.3 此控件的DatabaseLogin事件如下

private void environmentSettings1_DatabaseLogin(object sender, DatabaseLoginEventArgs e)

{

e.ConnectionString = textBox1.Text.Trim();

}

附:

(一)给报表传送查询语句 (加载后,运行前操作)

using FastReport.Data;

report1.Load("报表路径");

TableDataSource table = report1.GetDataSource("MyTable") as TableDataSource; //MyTable 即模板中表的名字

table.SelectCommand = "new SQL text"; //如果报表中含有多张表时,可采用此方法,给多张表指定查询语句

report1.Show();

(二)查询并访问报表中的控件(报表对象引用)

if (this.previewControl1.Report != null)

{

Report report = this.previewControl1.Report;

TextObject txt = report.FindObject("TextTitle") as TextObject;

if (txt != null)

{

MessageBox.Show(txt.Text);

txt.Text = txt.Text + "写";

report.Show();

}

}

(三)从报表中获取数据(报表对象引用)

if (this.previewControl1.Report != null)

{

Report report = this.previewControl1.Report;

DataSourceBase ds = report.GetDataSource("Table");

if (ds != null)

{

MessageBox.Show(ds.RowCount.ToString());

}

}

(四)关于报表总页数显示

一定要在报表--选项--双通道打上勾。否则显示的总页数为0

(脚本相关)

<基础>

1报表对象房问(可直接把对象拖到脚本区,系统会自动生成此对象的脚本)

1.0>返回数据表 DataSourceBase ds = Report.GetDataSource("Products"); (参数为表名)

1.1>房问数据列 Report.GetColumnValue("Products.Name"); (参数为表名.列名)

2系统变量访问

2.0>DateTime date = (DateTime)Report.GetVariableValue("Date");(参数为数据窗口,系统变量下的各个变量)

3合计值的访问

3.0>float a= Report.GetTotalValue("Total") //因为只有数值才能有合计,所以这里不用转换

4变量的访问 (变量是可以嵌套定义的)

4.0>int myParam = (int)Report.GetParameterValue("MyParameter");

4.1>int myParam =(int)Report.GetParameterValue("ParentParameter.ChildParameter"); (对嵌套变量的访问,采用父参数名.字参数名方式)

4.2>Report.SetParameterValue("MyParameter", 10); (设置变量值)

<应用>

<1> 隔行变色效果 (用到数据栏的事件BeforePrint,获取系统变量方法GetVariableValue 系统变量Row#)

private void Data1_BeforePrint(object sender, EventArgs e)

{

if (((Int32)Report.GetVariableValue("Row#")) % 2 == 0)

Data1.FillColor = Color.Gainsboro;

}

<2> 根据某列的内容,更改此列的颜色样式或者其他列的显示

(Text9 为报表中的数据列控件.用到控件的BeforePrint事件,获取控件值的方法GetColumnValue(参数为表的列名))

private void Text9_BeforePrint(object sender, EventArgs e)

{

if (((Decimal)Report.GetColumnValue("Table.RRT")) > 200)

{

Text9.TextColor = Color.Red;

TextObject to=Report.FindObject("Text7") as TextObject;

to.Text=Report.GetColumnValue("Table.RtpNam").ToString()+"(有点贵)";

}

}

<3>数据过滤

private void Data1_BeforePrint(object sender, EventArgs e)

{

if (((Int32)Report.GetVariableValue("Row#")) % 2 == 0)

Data1.FillColor = Color.Gainsboro;

if (((Decimal)Report.GetColumnValue("Table.RRT"))<=200) //过滤

Data1.Visible=false;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: