Fit Dynamic Table To Page.frx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Report ScriptLanguage="CSharp" ReportInfo.Description="This example demonstrates how to fit a dynamically generated TableObject to page's width. To do this:&#13;&#10;- use Table1.ResultTable property to access a generated table;&#13;&#10;- use the AfterCalcBounds event to fix table columns, just before printing the result table.&#13;&#10;See more details in the report's script." ReportInfo.Created="08/01/2008 14:01:38" ReportInfo.Modified="04/07/2023 14:34:41" 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. private void Table1_ManualBuild(object sender, EventArgs e)
  21. {
  22. // get the &quot;Customers&quot; datasource
  23. DataSourceBase customers = Report.GetDataSource(&quot;Customers&quot;);
  24. // init it
  25. customers.Init();
  26. // number of columns in the datasource
  27. int colCount = customers.Columns.Count;
  28. // print the table header which contains column titles. It's a row with index = 0.
  29. Table1.PrintRow(0);
  30. for (int i = 0; i &lt; colCount; i++)
  31. {
  32. // fill the cell with column title
  33. Cell1.Text = customers.Columns[i].Alias;
  34. // print it
  35. Table1.PrintColumn(0);
  36. }
  37. // now print a datasource content
  38. while (customers.HasMoreRows)
  39. {
  40. // print the table body. It's a row with index = 1.
  41. Table1.PrintRow(1);
  42. for (int i = 0; i &lt; colCount; i++)
  43. {
  44. // fill the cell with datasource column's data
  45. Cell2.Text = customers[customers.Columns[i]].ToString();
  46. // print it
  47. Table1.PrintColumn(0);
  48. }
  49. // move to the next row
  50. customers.Next();
  51. }
  52. // AfterCalcBounds event is fired after calculating table bounds, just before
  53. // printing the table. We will use it to correct the column's width.
  54. Table1.ResultTable.AfterCalcBounds += new EventHandler(ResultTable_AfterCalcBounds);
  55. }
  56. private void ResultTable_AfterCalcBounds(object sender, EventArgs e)
  57. {
  58. TableResult resultTable = sender as TableResult;
  59. float tableWidth = resultTable.Width;
  60. float pageWidth = Engine.PageWidth;
  61. if (tableWidth &gt; pageWidth)
  62. {
  63. // table is wider than page, correct the columns width
  64. float ratio = pageWidth / tableWidth;
  65. foreach (TableColumn column in resultTable.Columns)
  66. {
  67. column.AutoSize = false;
  68. column.Width *= ratio;
  69. }
  70. // this will recalculate table rows height
  71. resultTable.CalcHeight();
  72. }
  73. }
  74. }
  75. }
  76. </ScriptText>
  77. <Dictionary>
  78. <TableDataSource Name="Customers" ReferenceName="NorthWind.Customers" DataType="System.Int32" Enabled="true">
  79. <Column Name="CustomerID" DataType="System.String"/>
  80. <Column Name="CompanyName" DataType="System.String"/>
  81. <Column Name="ContactName" DataType="System.String"/>
  82. <Column Name="ContactTitle" DataType="System.String"/>
  83. <Column Name="Address" DataType="System.String"/>
  84. <Column Name="City" DataType="System.String"/>
  85. <Column Name="Region" DataType="System.String"/>
  86. <Column Name="PostalCode" DataType="System.String"/>
  87. <Column Name="Country" DataType="System.String"/>
  88. <Column Name="Phone" DataType="System.String"/>
  89. <Column Name="Fax" DataType="System.String"/>
  90. </TableDataSource>
  91. </Dictionary>
  92. <ReportPage Name="Page1" Watermark.Font="Arial, 60pt">
  93. <ReportTitleBand Name="ReportTitle1" Width="718.2" Height="47.25" CanGrow="true">
  94. <TextObject Name="Text1" Width="718.2" Height="37.8" Text="CUSTOMERS TABLE" HorzAlign="Center" VertAlign="Center" Font="Segoe UI, 14pt, style=Bold"/>
  95. </ReportTitleBand>
  96. <DataBand Name="Data1" Top="49.25" Width="718.2" Height="56.7">
  97. <TableObject Name="Table1" Width="94.5" Height="47.25" FixedRows="1" ManualBuildEvent="Table1_ManualBuild">
  98. <TableColumn Name="Column1" Width="94.5" AutoSize="true"/>
  99. <TableRow Name="Row1" Height="28.35" AutoSize="true">
  100. <TableCell Name="Cell1" Border.Lines="All" Border.Color="Gainsboro" Fill.Color="110, 145, 190" Text="Title" HorzAlign="Center" VertAlign="Center" Font="Segoe UI, 9pt, style=Bold" TextFill.Color="White"/>
  101. </TableRow>
  102. <TableRow Name="Row2" AutoSize="true">
  103. <TableCell Name="Cell2" Border.Lines="All" Border.Color="Gainsboro" Text="Data" VertAlign="Center" Font="Segoe UI, 9pt"/>
  104. </TableRow>
  105. </TableObject>
  106. </DataBand>
  107. <PageFooterBand Name="PageFooter1" Top="107.95" Width="718.2" Height="28.35" Fill.Color="WhiteSmoke">
  108. <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"/>
  109. </PageFooterBand>
  110. </ReportPage>
  111. </Report>