使用C1PrintDocument控件 > 数据绑定 > 数据绑定示例 > 使用汇总功能 |
以下代码扩展了之前的示例,介绍了为文档和分组使用汇总功能,该示例将和之前的代码做为一个整体:
Visual Basic
Visual Basic |
拷贝代码
|
---|---|
' 创建一个对每一个分组重复的Render区域: Dim ra As C1.C1Preview.RenderArea = New C1.C1Preview.RenderArea ra.Style.Borders.All = New C1.C1Preview.LineDef("2mm", Color.Blue) ra.DataBinding.DataSource = MyData.Generate(20, 0, True) ra.DataBinding.Grouping.Expressions.Add("Fields!GroupId.Value") ' 创建一个用于计算每一个分组中IntValue字段之和的汇总calc the sum of IntValue fields within each group: Dim agg As C1.C1Preview.DataBinding.Aggregate = New C1.C1Preview.DataBinding.Aggregate("Group_IntValue") ' 定义计算求和的表达式: agg.ExpressionText = "Fields!IntValue.Value" ' 指定该汇总操作应当针对当前分组范围: agg.Running = C1.C1Preview.DataBinding.RunningEnum.Group ' 指定汇总操作的数据源: agg.DataBinding = ra.DataBinding ''添加汇总至文档: doc.DataSchema.Aggregates.Add(agg) ' 创建一个用于计算每一个分组中IntValue字段之和的汇总calc the sum of IntValue fields over the whole document: agg = New C1.C1Preview.DataBinding.Aggregate("Total_IntValue") ' 定义用作计算求和的表达式: agg.ExpressionText = "Fields!IntValue.Value" ' Specify that aggregate should have document scope: agg.Running = C1.C1Preview.DataBinding.RunningEnum.All ' 指定汇总操作的数据源: agg.DataBinding = ra.DataBinding ''添加汇总至文档: doc.DataSchema.Aggregates.Add(agg) ' 添加分组页眉: Dim rt As C1.C1Preview.RenderText = New C1.C1Preview.RenderText rt.Text = "GroupId: [Fields!GroupId.Value]" rt.Style.BackColor = Color.Yellow ra.Children.Add(rt) ' 该呈现文本将打印分组记录;和可以看到的那样,分组汇总的值不仅仅可以在分组页脚中引用,也可以在分组页眉和分组内容区域引用 rt = New C1.C1Preview.RenderText rt.Text = "GroupId: [Fields!GroupId.Value]\rIntValue:[Fields!IntValue.Value]\rGroup_IntValue:[Aggregates!Group_IntValue.Value]\rTotal_IntValue:[Aggregates!Total_IntValue.Value]\rTatalNested_IntValue:[Aggregates!TatalNested_IntValue.Value]" rt.Style.Borders.Bottom = C1.C1Preview.LineDef.Default rt.Style.BackColor = Color.FromArgb(200, 210, 220) rt.DataBinding.DataSource = ra.DataBinding.DataSource ra.Children.Add(rt) ' 该汇总也在分组内进行计算,但是关联到内嵌对象的数据绑定: agg = New C1.C1Preview.DataBinding.Aggregate("TotalNested_IntValue") agg.ExpressionText = "Fields!IntValue.Value" agg.Running = RunningEnum.All agg.DataBinding = rt.DataBinding doc.DataSchema.Aggregates.Add(agg) ' 添加该区域至文档: doc.Body.Children.Add(ra) |
C#
C# |
拷贝代码
|
---|---|
// 创建一个对每一个分组重复的Render区域: RenderArea ra = new RenderArea(); ra.Style.Borders.All = new LineDef("2mm", Color.Blue); ra.DataBinding.DataSource = MyData.Generate(20, 0, true); ra.DataBinding.Grouping.Expressions.Add("Fields!GroupId.Value"); // 创建一个用于计算每一个分组中IntValue字段之和的汇总calc the sum of IntValue fields within each group: Aggregate agg = new Aggregate("Group_IntValue"); // 定义计算求和的表达式: agg.ExpressionText = "Fields!IntValue.Value"; // 指定该汇总操作应当针对当前分组范围: agg.Running = RunningEnum.Group; // 指定汇总操作的数据源: agg.DataBinding = ra.DataBinding; //添加汇总至文档: doc.DataSchema.Aggregates.Add(agg); // 创建一个用于计算每一个分组中IntValue字段之和的汇总: agg = new Aggregate("Total_IntValue"); // 定义用作计算求和的表达式: agg.ExpressionText = "Fields!IntValue.Value"; // Specify that aggregate should have document scope: agg.Running = RunningEnum.All; // 指定汇总操作的数据源: agg.DataBinding = ra.DataBinding; //添加汇总至文档: doc.DataSchema.Aggregates.Add(agg); // 添加分组页眉: RenderText rt = new RenderText(); rt.Text = "GroupId: [Fields!GroupId.Value]"; rt.Style.BackColor = Color.Yellow; ra.Children.Add(rt); // 该呈现文本将打印分组记录;和可以看到的那样,分组汇总的值不仅仅可以在分组页脚中引用,也可以在分组页眉和分组内容区域引用 : rt = new RenderText(); rt.Text = "GroupId: [Fields!GroupId.Value]\rIntValue:[Fields!IntValue.Value]\rGroup_IntValue:[Aggregates!Group_IntValue.Value]\rTotal_IntValue:[Aggregates!Total_IntValue.Value]\rTatalNested_IntValue:[Aggregates!TatalNested_IntValue.Value]"; rt.Style.Borders.Bottom = LineDef.Default; rt.Style.BackColor = Color.FromArgb(200, 210, 220); rt.DataBinding.DataSource = ra.DataBinding.DataSource; ra.Children.Add(rt); // 该汇总也在分组内进行计算,但是关联到内嵌对象的数据绑定: agg = new Aggregate("TotalNested_IntValue"); agg.ExpressionText = "Fields!IntValue.Value"; agg.Running = RunningEnum.All; agg.DataBinding = rt.DataBinding; doc.DataSchema.Aggregates.Add(agg); // 添加该区域至文档: doc.Body.Children.Add(ra); |
注意:也有可以在数据绑定的C1PrintDocuments使用的汇总类型,他们不需要声明在文档的汇总集合(Aggregates)。更多的细节和例子,请参见数据汇总主题。.