Example 58 : using TAdvGridWorkbook

vcl gridworkbook

The TAdvGridWorkbook is a component that facilitates working with multiple grids simultaneously (through sheets) but keeps a centralized single access to the grids properties and events. With this centralized approach, it is sufficient to write one event handler that will be used for all sheets in the workbook or set one property shared by all sheets in the workbook.

To make this centralized concept working, getting started with the component is only a little more involved than dropping the TAdvGridWorkbook on the form. After dropping the TAdvGridWorkbook on the form, we want easy access to the embedded grid properties and events. When dropping the TAdvGridWorkbook on the form though, only the TAdvGridWorkbook properties are available. To make the grid available for access in the form designer, choose for the form designer : View as text and after that a View as Form. An alternative way is to save and reload the form file. After this, you are all set to simultaneously access all properties of the grid and the workbook.

To programmatically access the grid in the workbook, first set the active sheet with the workbook ActiveSheet property and after this, use the AdvGridWorkbook.Grid property to access the active sheet's grid.  This is demonstrated in this sample code that loads multiple CSV files as sheets in the TAdvGridWorkbook:

procedure TForm1.Button1Click(Sender: TObject);
  i: integer;
  filelist: TStringList;
  fname: string;
  filelist := TStringList.Create;

  with AdvGridWorkBook1 do
    Grid.SaveFixedCells := false;

    for i := 1 to filelist.Count do
      fname := ExtractFileName(filelist.Strings[i - 1]);
      if pos('.',fname) > 0 then
        delete(fname,pos('.',fname), length(fname));

      Sheets.Add.Name := fname;
      ActiveSheet := i - 1;
      Grid.LoadFromCSV(filelist.Strings[i - 1]);
    ActiveSheet := 0;

After loading the data in the sheets, you can at all times directly access the grid with AdvGridWorkbook.grid. This refers to the grid used by the ActiveSheet, so programmatically performing a sort on the visible sheet can be done with

As such, all normal grid operations you are used to can be easily applied to the AdvGridWorkbook.

Delphi project & source files for downloading included in the main demos distribution for Delphi.