TAdvStringGrid

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);
var
  i: integer;
  filelist: TStringList;
  fname: string;
begin
  filelist := TStringList.Create;
  filelist.Add('c:\temp\cars.csv');
  filelist.Add('c:\temp\data.csv');
  filelist.Add('c:\temp\germany.csv');

  with AdvGridWorkBook1 do
  begin
    Sheets.Clear;
    Grid.SaveFixedCells := false;

    for i := 1 to filelist.Count do
    begin
      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]);
      Grid.AutoNumberCol(0);
      Grid.AutoNumberRow(0);
    end;
    ActiveSheet := 0;
  end;
  filelist.Free;
end;

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

AdvGridWorkbook.grid.QSort;
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.