文檔首頁>>Stimulsoft Reports.WinForms教程-2019>>【Stimulsoft Reports.WinForms教程】在運行時使用表創(chuàng)建一個新報表
【Stimulsoft Reports.WinForms教程】在運行時使用表創(chuàng)建一個新報表
【下載Stimulsoft Reports.Ultimate最新版本】
此示例顯示如何在運行時中使用表創(chuàng)建簡單報表。在此示例項目中,您可以為表設(shè)置一些屬性。使用表 組件,您可以創(chuàng)建一個包含標(biāo)題和總數(shù)而不包含其他波段的報表。在這種情況下,一些表行將是數(shù)據(jù)的頁眉和頁腳。首先,創(chuàng)建一個新報表并連接到數(shù)據(jù):
private void PrintDataGrid(DataGrid sender) { DataView dataView = (DataView)sender.DataSource; StiReport report = new StiReport(); report.ScriptLanguage = StiReportLanguageType.CSharp; //Add data to datastore report.RegData("view", dataView); //Fill dictionary report.Dictionary.Synchronize(); ...
接下來,在報表頁面上添加Table組件:
... StiPage page = report.Pages.Items[0]; //Create Table StiTable table = new StiTable(); table.Name = "Table1"; if (rbAWNone.Checked) table.AutoWidth = StiTableAutoWidth.None; else if (rbAWPage.Checked) table.AutoWidth = StiTableAutoWidth.Page; else table.AutoWidth = StiTableAutoWidth.Table; if (rbAWTNone.Checked) table.AutoWidthType = StiTableAutoWidthType.None; else if (rbAWTFullTable.Checked) table.AutoWidthType = StiTableAutoWidthType.FullTable; else table.AutoWidthType = StiTableAutoWidthType.LastColumns; ...
在表中定義多個Columns和Rows:
.. table.ColumnCount = 3; table.RowCount = 3; ...
在表中定義頁眉的多個行 和頁腳的行:
... table.HeaderRowsCount = 1; table.FooterRowsCount = 1; ...
定義Table組件的其他選項:
... table.Width = page.Width; table.Height = page.GridSize * 12; table.DataSourceName = "view" + dataView.Table.TableName; page.Components.Add(table); table.CreateCell(); table.TableStyleFX = new StiTable27StyleFX(); table.TableStyle = Stimulsoft.Report.Components.Table.StiTableStyle.Style59; int indexHeaderCell = 0; int indexDataCell = 3; ...
添加標(biāo)題的文本和對單元格中數(shù)據(jù)字段的引用:
... foreach (DataColumn column in dataView.Table.Columns) { //Set text on header StiTableCell headerCell = table.Components[indexHeaderCell] as StiTableCell; headerCell.Text.Value = column.Caption; headerCell.HorAlignment = StiTextHorAlignment.Center; headerCell.VertAlignment = StiVertAlignment.Center; StiTableCell dataCell = table.Components[indexDataCell] as StiTableCell; dataCell.Text.Value = "{view" + dataView.Table.TableName + "." + Stimulsoft.Report.CodeDom.StiCodeDomSerializator.ReplaceSymbols(column.ColumnName) + "}"; dataCell.Border = new StiBorder(StiBorderSides.All, Color.FromArgb(32, 178, 170), 1, StiPenStyle.Dash); indexHeaderCell++; indexDataCell++; } StiTableCell dataCheckBoxCell = table.Components[indexDataCell - 1] as StiTableCell; dataCheckBoxCell.CellType = StiTablceCellType.CheckBox; ...
在單元格中添加total的函數(shù):
... //Set text on footer StiTableCell footerCell = table.Components[table.Components.Count - 1] as StiTableCell; footerCell.Text.Value = "Count - {Count()}"; footerCell.Font = new Font("Arial", 15, FontStyle.Bold); footerCell.VertAlignment = StiVertAlignment.Center; footerCell.HorAlignment = StiTextHorAlignment.Center; ...
渲染報表并在查看器中顯示它:
... //Render without progress bar report.Render(false); report.Show(); }
示例代碼的結(jié)果如下圖所示: