Print Total in The Group Header.frx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Report ScriptLanguage="CSharp" DoublePass="true" ReportInfo.Description="Demonstrates how to use a script to print group's total in the group header (normally it is printed in the group footer). To create such a report:&#13;&#10;- the idea is to collect group totals on the first report pass, and to print the collected values on the second pass. So you have to set the report to double-pass (this can be done in the Report|Options... menu).&#13;&#10;- switch to Code tab and see the script for more details." ReportInfo.Created="01/17/2008 04:31:41" ReportInfo.Modified="03/30/2023 02:26:23" ReportInfo.CreatorVersion="1.0.0.0">
  3. <ScriptText>using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Windows.Forms;
  8. using System.Drawing;
  9. using System.Data;
  10. using FastReport;
  11. using FastReport.Data;
  12. using FastReport.Dialog;
  13. using FastReport.Barcode;
  14. using FastReport.Table;
  15. using FastReport.Utils;
  16. namespace FastReport
  17. {
  18. public class ReportScript
  19. {
  20. // The idea is to collect group totals on the first report pass, and to print
  21. // the collected values on the second pass.
  22. // The variable that we going to print in the group header.
  23. private int total;
  24. // The list that will hold totals for each group.
  25. private List&lt;int&gt; totals = new List&lt;int&gt;();
  26. // This handler is called before printing the group header. To create this handler:
  27. // - select the group header;
  28. // - go Properties window;
  29. // - press &quot;Events&quot; button;
  30. // - select &quot;BeforePrint&quot; event and doubleclick it.
  31. private void GroupHeader1_BeforePrint(object sender, EventArgs e)
  32. {
  33. // Note: ((Int32)Report.GetVariableValue(&quot;Row#&quot;)) is a current group number;
  34. // this string is automatically inserted when you drag &amp; drop
  35. // the &quot;Row#&quot; system variable from the Data Dictionary window to the script.
  36. // This value is not zero-based.
  37. int groupNumber = ((Int32)Report.GetVariableValue(&quot;Row#&quot;));
  38. // If this pass is final, get the collected value.
  39. if (Engine.FinalPass)
  40. total = totals[groupNumber - 1];
  41. }
  42. // This handler is called before printing the group footer.
  43. private void GroupFooter1_BeforePrint(object sender, EventArgs e)
  44. {
  45. // Note: Report.GetTotalValue(&quot;TotalProducts&quot;) is a total value;
  46. // this string is automatically inserted when you drag &amp; drop
  47. // the &quot;TotalProducts&quot; total from the Data Dictionary window to the script.
  48. int totalValue = (int)Report.GetTotalValue(&quot;TotalProducts&quot;);
  49. // if this pass is not final, add the total value to the totals list.
  50. if (!Engine.FinalPass)
  51. totals.Add(totalValue);
  52. }
  53. }
  54. }
  55. </ScriptText>
  56. <Styles>
  57. <Style Name="EvenRows" Fill.Color="216, 233, 220" Font="Arial, 10pt"/>
  58. </Styles>
  59. <Dictionary>
  60. <TableDataSource Name="Products" ReferenceName="NorthWind.Products" DataType="System.Int32" Enabled="true">
  61. <Column Name="ProductID" DataType="System.Int32"/>
  62. <Column Name="ProductName" DataType="System.String"/>
  63. <Column Name="SupplierID" DataType="System.Int32"/>
  64. <Column Name="CategoryID" DataType="System.Int32"/>
  65. <Column Name="QuantityPerUnit" DataType="System.String"/>
  66. <Column Name="UnitPrice" DataType="System.Decimal"/>
  67. <Column Name="UnitsInStock" DataType="System.Int16"/>
  68. <Column Name="UnitsOnOrder" DataType="System.Int16"/>
  69. <Column Name="ReorderLevel" DataType="System.Int16"/>
  70. <Column Name="Discontinued" DataType="System.Boolean" BindableControl="CheckBox"/>
  71. <Column Name="EAN13" DataType="System.String"/>
  72. </TableDataSource>
  73. <Total Name="TotalProducts" TotalType="Count" Evaluator="Data1" PrintOn="GroupFooter1"/>
  74. </Dictionary>
  75. <ReportPage Name="Page1" Watermark.Font="Arial, 60pt">
  76. <ReportTitleBand Name="ReportTitle1" Width="718.2" Height="37.8" CanGrow="true">
  77. <TextObject Name="Text1" Width="718.2" Height="37.8" Text="ALPHABETICAL PRODUCT LIST" HorzAlign="Center" VertAlign="Center" Font="Segoe UI, 14pt, style=Bold"/>
  78. </ReportTitleBand>
  79. <GroupHeaderBand Name="GroupHeader1" Top="39.8" Width="718.2" Height="37.8" BeforePrintEvent="GroupHeader1_BeforePrint" KeepWithData="true" Condition="[Products.ProductName].Substring(0,1)" SortOrder="None">
  80. <TextObject Name="Text7" Left="9.45" Top="9.45" Width="538.65" Height="28.35" Border.Color="LightSkyBlue" Fill.Color="DarkSeaGreen" Text="[[Products.ProductName].Substring(0,1)]" Padding="5, 0, 0, 0" VertAlign="Center" Font="Segoe UI, 12pt, style=Bold" TextFill.Color="White"/>
  81. <TextObject Name="Text10" Left="548.1" Top="9.45" Width="160.65" Height="28.35" Border.Color="LightSkyBlue" Fill.Color="DarkSeaGreen" Text="Total: [total]" Padding="5, 0, 0, 0" HorzAlign="Right" VertAlign="Center" Font="Segoe UI, 9pt, style=Bold" TextFill.Color="White"/>
  82. <DataBand Name="Data1" Top="79.6" Width="718.2" Height="18.9" DataSource="Products">
  83. <TextObject Name="Text4" Left="548.1" Width="160.65" Height="18.9" Border.Lines="Right" Border.Color="DarkSeaGreen" EvenStyle="EvenRows" Text="[Products.UnitPrice]" Format="Currency" Format.UseLocale="true" Format.DecimalDigits="2" HorzAlign="Right" VertAlign="Center" Font="Segoe UI, 9pt"/>
  84. <TextObject Name="Text2" Left="9.45" Width="538.65" Height="18.9" Border.Lines="Left" Border.Color="DarkSeaGreen" EvenStyle="EvenRows" Text="[Products.ProductName]" VertAlign="Center" Font="Segoe UI, 9pt"/>
  85. <Sort>
  86. <Sort Expression="[Products.ProductName]"/>
  87. </Sort>
  88. </DataBand>
  89. <GroupFooterBand Name="GroupFooter1" Top="100.5" Width="718.2" Height="28.35" BeforePrintEvent="GroupFooter1_BeforePrint" KeepWithData="true">
  90. <TextObject Name="Text8" Left="9.45" Width="699.3" Height="18.9" Border.Lines="Left, Right, Bottom" Border.Color="DarkSeaGreen" Text="Total products: [TotalProducts]" HorzAlign="Right" VertAlign="Center" Font="Segoe UI, 9pt, style=Bold"/>
  91. </GroupFooterBand>
  92. </GroupHeaderBand>
  93. <PageFooterBand Name="PageFooter1" Top="130.85" Width="718.2" Height="28.35" Fill.Color="WhiteSmoke">
  94. <TextObject Name="Text9" Left="614.25" Width="94.5" Height="28.35" Text="[PageN]" HorzAlign="Right" VertAlign="Center" Font="Segoe UI, 9pt"/>
  95. <TextObject Name="Text12" Left="9.45" Width="217.35" Height="28.35" Cursor="Hand" Hyperlink.Value="https://www.fast-report.com/en/product/fast-report-net/" Text="Generated by FastReport" VertAlign="Center" Font="Segoe UI, 9pt, style=Underline" TextFill.Color="Blue"/>
  96. </PageFooterBand>
  97. </ReportPage>
  98. </Report>