【问题标题】:Programmatically create a fastreport.net TableObject以编程方式创建 fastreport.net TableObject
【发布时间】:2016-05-09 05:29:17
【问题描述】:

我在我的 WinForm 应用程序中使用 FastReport.Net 作为我的报告工具。在那里,我需要动态创建一个 TableObject 并使用数据源使用数据填充它。我正在寻找一个示例 C# 代码。

【问题讨论】:

  • 那么你应该参考their documentation
  • 感谢您的快速回复..但我在文档中找不到..

标签: c# winforms fastreport


【解决方案1】:

您可以在 Demo in Table 选项卡中找到它。 以下内容可能有用:

private void Table1_ManualBuild(object sender, EventArgs e)
    {
      // get the data source by its name
      DataSourceBase columnData = Report.GetDataSource("Employees");
      // init the data source
      columnData.Init();

      // print the first table column - it is a header
      Table1.PrintColumn(0);
      // each PrintColumn call must be followed by either PrintRow or PrintRows call
      // to print cells on the column
      Table1.PrintRows();

      // now enumerate the data source and print the table body
      while (columnData.HasMoreRows)
      {
        // print the table body  
        Table1.PrintColumn(1);
        Table1.PrintRows();

        // go next data source row
        columnData.Next();
      }

      // print the last table column - it is a footer
      Table1.PrintColumn(2);
      Table1.PrintRows();
    }

private void Table1_ManualBuild(object sender, EventArgs e)
{
  // get the master data source
  DataSourceBase masterData = Report.GetDataSource("Categories");
  // get the detail data source
  DataSourceBase detailData = Report.GetDataSource("Products");

  // init the master data source
  masterData.Init();

  while (masterData.HasMoreRows)
  {
    // print first 3 rows that contains data from master data source
    Table1.PrintRow(0);
    Table1.PrintColumns();
    Table1.PrintRow(1);
    Table1.PrintColumns();
    Table1.PrintRow(2);
    Table1.PrintColumns();

    // init the detail data source. Pass masterData to allow master-detail relation
    detailData.Init(masterData);

    // print detail header
    Table1.PrintRow(3);
    Table1.PrintColumns();

    // print detail rows
    while (detailData.HasMoreRows)
    {
      // print the detail row
      Table1.PrintRow(4);
      Table1.PrintColumns();

      // go next data source row
      detailData.Next();
    }

    // print the detail footer row
    Table1.PrintRow(5);
    Table1.PrintColumns();
    Table1.PrintRow(6);
    Table1.PrintColumns();

    // go next data source row
    masterData.Next();
  }

【讨论】:

    【解决方案2】:

    尝试以下代码以在 FastReport 中创建动态 TableObject

        Report report = new Report();
        
        ReportPage page = new ReportPage();
    
        DataBand dataBand = new DataBand();
        dataBand.Name = "dataBand";
        dataBand.Height = Units.Centimeters * 24.7f;
        int columnCount = 4;
        int rowCount = 20;
        
                    for (int i = 0; i < columnCount; i++)
                    {
                        TableColumn tableColumn = new TableColumn();
                        tableColumn.Width = Units.Centimeters * 4.75f;
                        table.Columns.Insert(i, tableColumn);
                    }
        
                    // create DataBand --> create table --> and then creating rows and cells
                    for (int row = 0; row < rowCount; row++)
                    {
                        TableRow tableRow = new TableRow();
                        tableRow.Height = row == 0 ? Units.Centimeters * 1f : Units.Centimeters * 1f;
                        table.Rows.Insert(row, tableRow);
        
                        for (int col = 0; col < columnCount; col++)
                        {
                            table[col, row].Border.Lines = BorderLines.All;
                            table[col, row].HorzAlign = HorzAlign.Center;
                            table[col, row].VertAlign = VertAlign.Center;
                            table[col, row].ParagraphFormat = new ParagraphFormat();
                            table[col, row].ParagraphFormat.LineSpacing = Units.Centimeters * .1f;
                            table[col, row].Font = new Font("Tahoma", 10, FontStyle.Regular);
        
                            if (row % 2 == 0)
                            {
                                table[col, row].FillColor = Color.LightGray;
                            }
        
                            if (row >= 1)
                            {
                                table[col, row].Text = "Testing Obj";
                            }                   
                            else if (row == 0)
                            {
                                table[col, row].Text = "Column_" + col.ToString();
                                table[col, row].FillColor = Color.LightBlue;
                                table[col, row].Font = new Font("Tahoma", 12, FontStyle.Bold);
                            }
                        }
        
                    }
    dataBand.AddChild(table);
    page.Bands.Add(dataBand); 
    
    report.Pages.Add(page);
    

    【讨论】:

      猜你喜欢
      • 2013-03-28
      • 2013-07-25
      • 2013-07-22
      • 2018-06-04
      • 2015-03-17
      • 2013-11-24
      • 2016-01-01
      • 2011-03-04
      • 2012-09-17
      相关资源
      最近更新 更多