FastReport(RAD Studio XE7 C ++ Builder) - 如何创建总和公式?

时间:2014-10-03 01:05:59

标签: c++builder fastreport

我正在使用Embarcadero RAD Studio XE7,C ++ Builder和FastReport 5.我创建了一个非常简单的FastReport,它在表格中添加了一些值并打印出一个总数。我无法得到总和公式的语法正确,因此它不断抛出访问冲突错误。

要在RAD Studio XE7中重现此问题:

  1. 打开RAD Studio XE7并转到文件 - >新 - > VCL Forms Application - C ++ Builder
  2. 将名为ClientDataSet1的TClientDataSet组件拖到窗体上。将名为frxReport1的TfrxReport组件拖到窗体上。将名为frxDBDataset1的TfrxDBDataset拖到窗体上。
  3. 将名为Button1的TButton拖到窗体上,然后双击它以创建OnClick事件处理程序。
  4. 将以下行添加到Button1Click事件处理程序:

    // Create a simple dataset.
    
    ClientDataSet1->FieldDefs->Clear();
    ClientDataSet1->FieldDefs->Add("ID", ftInteger, 0, false);
    ClientDataSet1->FieldDefs->Add("Status", ftString, 10, false);
    ClientDataSet1->FieldDefs->Add("Created", ftDate, 0, false);
    ClientDataSet1->FieldDefs->Add("Volume", ftInteger, 0, false);
    
    try
    {
      ClientDataSet1->CreateDataSet();
    }
    catch(Exception& e)
    {
      ShowMessage("ERROR: '" + e.Message + "'");
      return;
    }
    
    ClientDataSet1->Open();
    for (int i = 0; i < 10; ++i)
    {
      ClientDataSet1->Append();
      ClientDataSet1->FieldByName("ID")->AsInteger = i;
      ClientDataSet1->FieldByName("Status")->AsString = "Code" + String(i);
      ClientDataSet1->FieldByName("Created")->AsDateTime = Now();
      ClientDataSet1->FieldByName("Volume")->AsInteger = Random(1000);
    
      try
      {
        ClientDataSet1->Post();
      }
      catch(Exception& e)
      {
        ShowMessage("ERROR: '" + e.Message + "'");
        ClientDataSet1->Close();
        return;
      }
    }
    
    // Dataset created successfully, now create Fast Report that outputs that dataset
    
    frxReport1->Clear();
    frxDBDataset1->DataSet = (TDataSet*)ClientDataSet1;
    
    frxReport1->DataSets->Add(frxDBDataset1);
    
    TfrxDataPage* DataPage = new TfrxDataPage(frxReport1);
    DataPage->CreateUniqueName();
    
    TfrxReportPage* Page = new TfrxReportPage(frxReport1);
    Page->CreateUniqueName();
    
    // set sizes of fields, paper and orientation to defaults
    Page->SetDefaults();
    Page->Orientation = poPortrait;
    
    TfrxReportTitle* HeaderBand = new TfrxReportTitle(Page);
    HeaderBand->CreateUniqueName();
    HeaderBand->Top = 0;
    HeaderBand->Height = 20;
    
    TfrxMemoView* Memo = new TfrxMemoView(HeaderBand);
    Memo->CreateUniqueName();
    Memo->Text = "Generic Report";
    Memo->SetBounds(0, 0, 200, 20);
    
    TfrxHeader* ColumnHeaderBand;
    ColumnHeaderBand = new TfrxHeader(Page);
    ColumnHeaderBand->CreateUniqueName();
    ColumnHeaderBand->Top = HeaderBand->Top + HeaderBand->Height;
    ColumnHeaderBand->Height = 20;
    
    TfrxMasterData* DataBand = new TfrxMasterData(Page);
    DataBand->Name = "DataBand";
    DataBand->DataSet = frxDBDataset1;
    DataBand->Top = ColumnHeaderBand->Top + ColumnHeaderBand->Height;
    DataBand->Height = 20;
    
    TfrxMemoView* mField;
    for (int i = 0; i < DataBand->DataSet->FieldsCount(); ++i)
    {
      const String fieldname = ClientDataSet1->Fields->Fields[i]->FieldName;
    
      mField = new TfrxMemoView(ColumnHeaderBand);
      mField->CreateUniqueName();
      mField->SetBounds(i * 100, 0, 100, 20);
      mField->Text = fieldname;
      mField->HAlign = haCenter;
    
      // Now do the actual data
      mField = new TfrxMemoView(DataBand);
      mField->CreateUniqueName();
      mField->DataSet = DataBand->DataSet;
      mField->DataField = fieldname;
      mField->SetBounds(i * 100, 0, 100, 20);
      mField->HAlign = haRight;
    }
    
    // Now do footer band. This will hold the total
    TfrxBand* FooterBand = new TfrxFooter(Page);
    FooterBand->CreateUniqueName();
    FooterBand->Top = DataBand->Top + DataBand->Height;
    FooterBand->Height = HeaderBand->Height;
    
    TfrxMemoView* totals = new TfrxMemoView(FooterBand);
    totals->Top = 0;
    totals->Left = 0;
    totals->Height = 20;
    totals->Align = baWidth;
    
    bool is_error = false;
    try
    {
      // ALL OF THESE LINES CAUSE THE ACCESS VIOLATION
    
      // Create a summation function that displays the volume total
      totals->Text = "Totals: [Sum(<ClientDataSet1.Volume>, MyDataBand, 1)]";
      //totals->Text = "Totals: [Sum(<ClientDataSet1.'volume'>,MyDataBand,1)]";
      //totals->Text = "Totals: [Sum(<ClientDataSet1.\"volume\">,MyDataBand,1)]";
      //totals->Text = "Totals: [Sum(<ClientDataSet1.""volume"">,MyDataBand,1)]";
      //totals->Text = "Totals: [Sum(<ClientDataSet1.''volume''>,MyDataBand,1)]";
      //totals->Text = "Totals: [Sum(<ClientDataSet1.\'volume\'>,MyDataBand,1)]";
    }
    catch(Exception& e)
    {
      ShowMessage("ERROR: '" + e.Message + "'");
      is_error = true;
    }
    
    if (!is_error)
    {
      frxReport1->ShowReport(true);
    }
    
    ClientDataSet1->Close();
    
    ShowMessage("Program complete!");
    
  5. 编译并运行程序。 try块中的代码将引发访问冲突。为什么会这样?创建总和公式的正确语法是什么?

  6. 更新

    我通过显式设置frxDBDataset1的名称来修改代码:

        frxReport1->Clear();
        frxDBDataset1->DataSet = (TDataSet*)ClientDataSet1;
        frxDBDataset1->Name = "frxDBDataset1"; // line added
        frxReport1->DataSets->Add(frxDBDataset1);
    

    我还将公式行更改为以下内容:

        totals->Text = "Totals: [Sum(<frxDBDataset1.\"Volume\">, DataBand, 1)]";
    

    但我仍然遇到访问冲突。

2 个答案:

答案 0 :(得分:0)

我能够毫无问题地运行以下链接示例,其中包括我们之前评论过的修改。

https://www.dropbox.com/s/6grmajvoy9ijxfh/SO_test1.7z?dl=0

答案 1 :(得分:0)

转到项目 - &gt;选项 - &gt;包裹 - &gt;运行时包。将“与运行时包链接”设置为False。

此选项的默认值对于C ++ Builder为True,对于Delphi为False。这解释了为什么等效代码在Delphi中工作,但在C ++ Builder中不起作用。