FastReport打印DataTable
数据DataTable:
1.创建2行1列Table:
2.添加事件:
3.添加代码:
4. 效果
附代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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(); } }
安装的FastReport官方事例:
C:\Program Files (x86)\FastReports\FastReport.Net\Demos\Reports\Print DataTable
所有列保持在一页:
事件:
Table2.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(); } }