123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?xml version="1.0" encoding="utf-8"?>
- <Report ScriptLanguage="CSharp" ReportInfo.Description="This example demonstrates how to fit a dynamically generated TableObject to page's width. To do this: - use Table1.ResultTable property to access a generated table; - use the AfterCalcBounds event to fix table columns, just before printing the result table. 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">
- <ScriptText>using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Drawing;
- using System.Data;
- using FastReport;
- using FastReport.Data;
- using FastReport.Dialog;
- using FastReport.Barcode;
- using FastReport.Table;
- using FastReport.Utils;
- namespace FastReport
- {
- public class ReportScript
- {
- private void Table1_ManualBuild(object sender, EventArgs e)
- {
- // get the "Customers" datasource
- DataSourceBase customers = Report.GetDataSource("Customers");
- // init it
- customers.Init();
-
- // number of columns in the datasource
- int colCount = customers.Columns.Count;
-
- // print the table header which contains column titles. It's a row with index = 0.
- Table1.PrintRow(0);
- for (int i = 0; i < colCount; i++)
- {
- // fill the cell with column title
- Cell1.Text = customers.Columns[i].Alias;
- // print it
- Table1.PrintColumn(0);
- }
-
- // now print a datasource content
- while (customers.HasMoreRows)
- {
- // print the table body. It's a row with index = 1.
- Table1.PrintRow(1);
- for (int i = 0; i < colCount; i++)
- {
- // fill the cell with datasource column's data
- Cell2.Text = customers[customers.Columns[i]].ToString();
- // print it
- Table1.PrintColumn(0);
- }
-
- // move to the next row
- customers.Next();
- }
-
- // AfterCalcBounds event is fired after calculating table bounds, just before
- // printing the table. We will use it to correct the column's width.
- Table1.ResultTable.AfterCalcBounds += new EventHandler(ResultTable_AfterCalcBounds);
- }
-
- private void ResultTable_AfterCalcBounds(object sender, EventArgs e)
- {
- TableResult resultTable = sender as TableResult;
- float tableWidth = resultTable.Width;
- float pageWidth = Engine.PageWidth;
-
- if (tableWidth > pageWidth)
- {
- // table is wider than page, correct the columns width
- float ratio = pageWidth / tableWidth;
- foreach (TableColumn column in resultTable.Columns)
- {
- column.AutoSize = false;
- column.Width *= ratio;
- }
-
- // this will recalculate table rows height
- resultTable.CalcHeight();
- }
- }
- }
- }
- </ScriptText>
- <Dictionary>
- <TableDataSource Name="Customers" ReferenceName="NorthWind.Customers" DataType="System.Int32" Enabled="true">
- <Column Name="CustomerID" DataType="System.String"/>
- <Column Name="CompanyName" DataType="System.String"/>
- <Column Name="ContactName" DataType="System.String"/>
- <Column Name="ContactTitle" DataType="System.String"/>
- <Column Name="Address" DataType="System.String"/>
- <Column Name="City" DataType="System.String"/>
- <Column Name="Region" DataType="System.String"/>
- <Column Name="PostalCode" DataType="System.String"/>
- <Column Name="Country" DataType="System.String"/>
- <Column Name="Phone" DataType="System.String"/>
- <Column Name="Fax" DataType="System.String"/>
- </TableDataSource>
- </Dictionary>
- <ReportPage Name="Page1" Watermark.Font="Arial, 60pt">
- <ReportTitleBand Name="ReportTitle1" Width="718.2" Height="47.25" CanGrow="true">
- <TextObject Name="Text1" Width="718.2" Height="37.8" Text="CUSTOMERS TABLE" HorzAlign="Center" VertAlign="Center" Font="Segoe UI, 14pt, style=Bold"/>
- </ReportTitleBand>
- <DataBand Name="Data1" Top="49.25" Width="718.2" Height="56.7">
- <TableObject Name="Table1" Width="94.5" Height="47.25" FixedRows="1" ManualBuildEvent="Table1_ManualBuild">
- <TableColumn Name="Column1" Width="94.5" AutoSize="true"/>
- <TableRow Name="Row1" Height="28.35" AutoSize="true">
- <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"/>
- </TableRow>
- <TableRow Name="Row2" AutoSize="true">
- <TableCell Name="Cell2" Border.Lines="All" Border.Color="Gainsboro" Text="Data" VertAlign="Center" Font="Segoe UI, 9pt"/>
- </TableRow>
- </TableObject>
- </DataBand>
- <PageFooterBand Name="PageFooter1" Top="107.95" Width="718.2" Height="28.35" Fill.Color="WhiteSmoke">
- <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"/>
- </PageFooterBand>
- </ReportPage>
- </Report>
|