现在的位置: 首页 > 综合 > 正文

Report报表学习系列三

2013年05月13日 ⁄ 综合 ⁄ 共 5114字 ⁄ 字号 评论关闭

昨天写了用C#做最基本的报表,并手动配置数据源,这篇主要讲报表参数设置和建立子表,分组。

一 建立报表参数    

    打开昨天的项目,在Report文件夹中新建报表rptStudent .rdlc,拖入一张表,点击报表-报表参数--参数--添加,参数名称Cno,确定

二 为子报表添加数据集

    依然是报表--数据源--把RptDataSet_Student添加到报表,确定.

三 设计子报表

    添加分组,如图1

图1
分组依据为ClassID,包括组头和组尾。如图2

图2
作如下设置
其中的=Count(Fields!Sno.Value) 可以右击单元格,表达式--常见函数--聚合,选择Count(),再在Count()的括号中选择字段,双击Sno。
这里顺便讲一下分组,其实分组就像SQL里面的聚合函数,通过一定的分组依据,把数据划分成相同性质的若干小组,就像这里用班号,把学生数据划分为若干个班级。
然后选择中表格,右键,选择属性的筛选器,设置(=Fields!ClassID.Value)=(=Parameters!.Cid.value)
四 在主报表中的设置.
   打开主报表rptClass,从工具箱里面拖一张子表放入组尾(最好把尾组的一行单元格合并),重命名为subRptStudent
  设置子报表subRptStudent控件的ReportName为rptStudent,右键--属性--参数--Cid值为=Fields!SystemKey.Value,如图

 

五编写Student的数据访问类
    首先,在DataSource文件中添加Studnet的数据处理类StudentDataSource.类的内容如下,它为报表提供数据源实例。
public class StudentDataSource
{
public DataTable GetStudentData()
{
DataTable dt
= new DataTable();
SqlConnection conn
= new SqlConnection(ConfigurationManager.ConnectionStrings["ReportingAPP.Properties.Settings.RptDBConnectionString"].ToString());
StringBuilder cmdText
= new StringBuilder();
cmdText.Append(
"SELECT [SystemKey] ,[Sno],[Sname],[ClassID] FROM [Student]");
try
{
conn.Open();
SqlCommand cmd
= new SqlCommand(cmdText.ToString(), conn);
dt.Load(cmd.ExecuteReader());
}
catch (Exception ex)
{
}
finally
{
conn.Close();
}
return dt;
}
}
然后在主窗体中添加
六  为子报表添加子报表的本地处理事件的委托方法。
在主窗体中添加如下方法
        private void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
{
e.DataSources .Add (
new ReportDataSource("RptDataSet_Student", studentDataSource.GetStudentData())
);
}
七 为主报表添加添加子报表的本地处理事件
    在主窗体的Load事件中,添加如下语句
 private void FrmRptMain_Load(object sender, EventArgs e)
{
this.rptViewMain.LocalReport.SubreportProcessing +=
new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);//如果没有这个会报子报表无法显示的错误
this.rptViewMain.LocalReport.ReportEmbeddedResource = "ReportingAPP.Report.rptClass.rdlc";
this.rptViewMain.LocalReport.DataSources.Add(
new ReportDataSource("RptDataSet_Class",dataSource.GetClassData ())
);
this.rptViewMain.RefreshReport();
}
好吧,现在我是迫不及待的按F5了。。。。。。并没有显示学生名字。。。rptStudent中的表要把设置DataSetName为RptDataSet_Student,输出数据选择为是。
现在可以了。运行结果如下

现在来简单的总结一下子报表的处理
1)报立子报表的RDLC文件,建立参数,选择数据集,写好筛选器的条件,为报表里的表选择好DataSetName,输出数据设置为是,并按所需设置子报表格式和内容
2)在主报表中添加子报表控件,选择子报表控件的ReportName为刚建立好的RDLC子报表,设置参数。
3)为子报表写数据源提供数据实例(编写相关的DataSource类)
4)编写本地报表的委托处理方法
  private void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
{
e.DataSources .Add (
new ReportDataSource("RptDataSet_Student", studentDataSource.GetStudentData())
);
}
5)添加子报表的本地处理事件 this.rptViewMain.LocalReport.SubreportProcessing +=
                 new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);
好像有点累,今天就到这里吧,明天写钻取报表。

昨天写了用C#做最基本的报表,并手动配置数据源,这篇主要讲报表参数设置和建立子表,分组。

一 建立报表参数    

    打开昨天的项目,在Report文件夹中新建报表rptStudent .rdlc,拖入一张表,点击报表-报表参数--参数--添加,参数名称Cno,确定

二 为子报表添加数据集

    依然是报表--数据源--把RptDataSet_Student添加到报表,确定.

三 设计子报表

    添加分组,如图1

图1
分组依据为ClassID,包括组头和组尾。如图2

图2
作如下设置
其中的=Count(Fields!Sno.Value) 可以右击单元格,表达式--常见函数--聚合,选择Count(),再在Count()的括号中选择字段,双击Sno。
这里顺便讲一下分组,其实分组就像SQL里面的聚合函数,通过一定的分组依据,把数据划分成相同性质的若干小组,就像这里用班号,把学生数据划分为若干个班级。
然后选择中表格,右键,选择属性的筛选器,设置(=Fields!ClassID.Value)=(=Parameters!.Cid.value)
四 在主报表中的设置.
   打开主报表rptClass,从工具箱里面拖一张子表放入组尾(最好把尾组的一行单元格合并),重命名为subRptStudent
  设置子报表subRptStudent控件的ReportName为rptStudent,右键--属性--参数--Cid值为=Fields!SystemKey.Value,如图

 

五编写Student的数据访问类
    首先,在DataSource文件中添加Studnet的数据处理类StudentDataSource.类的内容如下,它为报表提供数据源实例。
public class StudentDataSource
{
public DataTable GetStudentData()
{
DataTable dt
= new DataTable();
SqlConnection conn
= new SqlConnection(ConfigurationManager.ConnectionStrings["ReportingAPP.Properties.Settings.RptDBConnectionString"].ToString());
StringBuilder cmdText
= new StringBuilder();
cmdText.Append(
"SELECT [SystemKey] ,[Sno],[Sname],[ClassID] FROM [Student]");
try
{
conn.Open();
SqlCommand cmd
= new SqlCommand(cmdText.ToString(), conn);
dt.Load(cmd.ExecuteReader());
}
catch (Exception ex)
{
}
finally
{
conn.Close();
}
return dt;
}
}
然后在主窗体中添加
六  为子报表添加子报表的本地处理事件的委托方法。
在主窗体中添加如下方法
        private void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
{
e.DataSources .Add (
new ReportDataSource("RptDataSet_Student", studentDataSource.GetStudentData())
);
}
七 为主报表添加添加子报表的本地处理事件
    在主窗体的Load事件中,添加如下语句
 private void FrmRptMain_Load(object sender, EventArgs e)
{
this.rptViewMain.LocalReport.SubreportProcessing +=
new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);//如果没有这个会报子报表无法显示的错误
this.rptViewMain.LocalReport.ReportEmbeddedResource = "ReportingAPP.Report.rptClass.rdlc";
this.rptViewMain.LocalReport.DataSources.Add(
new ReportDataSource("RptDataSet_Class",dataSource.GetClassData ())
);
this.rptViewMain.RefreshReport();
}
好吧,现在我是迫不及待的按F5了。。。。。。并没有显示学生名字。。。rptStudent中的表要把设置DataSetName为RptDataSet_Student,输出数据选择为是。
现在可以了。运行结果如下

现在来简单的总结一下子报表的处理
1)报立子报表的RDLC文件,建立参数,选择数据集,写好筛选器的条件,为报表里的表选择好DataSetName,输出数据设置为是,并按所需设置子报表格式和内容
2)在主报表中添加子报表控件,选择子报表控件的ReportName为刚建立好的RDLC子报表,设置参数。
3)为子报表写数据源提供数据实例(编写相关的DataSource类)
4)编写本地报表的委托处理方法
  private void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
{
e.DataSources .Add (
new ReportDataSource("RptDataSet_Student", studentDataSource.GetStudentData())
);
}
5)添加子报表的本地处理事件 this.rptViewMain.LocalReport.SubreportProcessing +=
                 new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);
好像有点累,今天就到这里吧,明天写钻取报表。
【上篇】
【下篇】

抱歉!评论已关闭.