How to print a stringgrid (incl. formating)?

I want to print a TStringGrid - and I want the output to look like the stringgrid. The columns in the print should have the same width as the columns in the stringgrid. The colours should be the same and the font should be the same and so on.

Can someone give me a hint on how I could do that??

Any response is most welcome.

Regards,
Mikael

Comments

  • diaswdiasw Germany
    edited 9:43AM
    Hi,
    I don't think that FR can do this. I guess you need a Stringgrid which has a print-component. I think DevExpress with its XtraGrid has such a component.

    hth
    Sascha
  • edited January 2005
    Hmm... Thanks for the reply.

    Perhaps FR is not able to print a stringgrid with formating automatically, but I can not believe that is in not possbile to do it somehow.

    If we take a look at the PrintStringGrid demo, I wonder if it is possible
    to modify the Cross variable, so that I can change properties of
    a cell - and thereby make the print look like a stringgrid
    procedure TForm1.frxReport1BeforePrint(c: TfrxReportComponent);
    var
     ? Cross: TfrxCrossView;
     ? i, j: Integer;
    begin
     ? if c is TfrxMemoView then
     ? begin
     ?  ?  if c.Name = 'meReportTitle' then
     ?  ?  begin
     ?  ?  ?  ? TfrxMemoView(c).Memo.Text:= edTitel.text;
     ?  ?  end;
     ? end;
    
     ? if c is TfrxCrossView then
     ? begin
     ?  ? Cross := TfrxCrossView(c);
     ?  ? for i := 1 to StringGrid1.RowCount do
     ?  ? begin
     ?  ?  ? for j := 1 to StringGrid1.ColCount do
     ?  ?  ? begin
     ?  ?  ?  ? Cross.AddValue([i], [j], [StringGrid1.Cells[i - 1, j - 1]]);
     ?  ?  ?  ? // Can Cross be used to set the width, color and such for a cell?
     ?  ?  ? end;
     ?  ? end;
     ? end;
    end;
    

    I am a complete newbie to FR, so any help is most welcome.

    Regards,
    Mikael
  • edited 9:43AM
    You can use vertical bands if you want to print more customizable grid. See example in the demos\main, "Old-style cross-tab" and "Calendar" reports.
  • edited 9:43AM
    Thanks. I will try that.

    Regards,
    Mikael
  • edited 9:43AM
    There's yet another way to do what you want.
    It is very resource-hungry, but also the best-looking one.
    You can paint your string grid to a custom bitmap (see PaintTo method), then either paste it to the database or use one of those in-memory TDataSet inheritors (for example, RxMemoryData).
  • edited 9:43AM
    I need to print very large grids, so the bitmap approach is properly not the right solution.

    Here is what I plan to do:

    1. Have an empty fastreport component on my form.
    2. Design the report by code. Something like this:
    var
      page: TfrxReportPage;
      band: TfrxBand;
      memo: TfrxMemoView;
      cross: TfrxCrossView;
      masterBand: TfrxMasterData;
      i, x: Integer;
    begin
      // Make sure that report is clean.
      report.Clear;
    
      // Set dataset range properties
      dataset.RangeEnd:= reCount;
      dataset.RangeEndCount:= grid.RowCount;
    
      // Add dataset to report
      report.DataSets.Add(dataset);
    
      // Add a page
      page:= TfrxReportPage.Create(report);
      page.CreateUniqueName;
      page.SetDefaults;
      page.Orientation:= poLandscape;
    
      // Add a report title band
      band:= TfrxReportTitle.Create(page);
      band.CreateUniqueName;
      band.Top:= 0;
      band.Height:= mm2pixel(8);
    
      // Add object to the report title band
      memo:= TfrxMemoView.Create(band);
      memo.CreateUniqueName;
      memo.Text:= title;
      memo.Height:= mm2pixel(8);
      memo.Align := baWidth;
    
      // Add masterdata band
      masterBand:= TfrxMasterData.Create(page);
      masterBand.CreateUniqueName;
      masterBand.DataSet:= dataset;
      masterBand.Top:= band.Top+band.Height+1;
      masterBand.Height:= grid.DefaultRowHeight;
    
      // Add objects on master data
      x:= 0;
      for i:= 0 to grid.ColCount-1 do
      begin
        memo:= TfrxMemoView.Create(masterBand);
        memo.CreateUniqueName;
        // Connect to data
        memo.DataSet:= dataset;
        memo.DataField:= 'ColNo'+IntToStr(i);
        memo.SetBounds(x, 0, grid.ColWidths[i], grid.DefaultRowHeight);
        x:= x+grid.ColWidths[i];
        memo.Frame.Typ:= [ftLeft, ftRight, ftTop, ftBottom];
        memo.GapX:= 3;
        memo.GapY:= 2;
      end;
    


    3. Use report???s OnBeforePrint event to populate the report with data. Something like this:
    procedure TForm1.beforePrint(Sender: TfrxReportComponent);
    var
      col, row: Integer;
    begin
      if (Sender is TfrxMemoView) and (Copy(TfrxMemoView(Sender).DataField, 1, 5) = 'ColNo') then
      begin
        row:= frxUserDataSet5.RecNo;
        col:= StrToInt(Copy(TfrxMemoView(Sender).DataField, 6, length(TfrxMemoView(Sender).DataField)));
        case mlProfGrid1.Cells[col, row].TextAlignment of
          taLeftJustify: TfrxMemoView(Sender).HAlign:= haLeft;
          taRightJustify: TfrxMemoView(Sender).HAlign:= haRight;
          taCenter: TfrxMemoView(Sender).HAlign:= haCenter;
        end;
        TfrxMemoView(Sender).Value:= mlProfGrid1.Cells[col, row].Value;
      end;
    

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.