TAdvStringGrid

Example 6 : Imagelist images + sorting

vcl grid sorting

In demo 6, attention is paid to sorting several types of data, including custom sorting. To create some data for grid, a directory browser is implemented. In this simple example, the FindFileFirst and FindFileNext Win32 API functions are used to get the files of the current directory. This is done in the ButtonClick handler. As an extension of this application, you might want to enhance it by handling double-clicks on folders to browse the directory structure. Additionally, the SHGetFileInfo call is used to get the file typename and index of the file icon in the system imagelist. This system imagelist is assigned to the GridImages property in the FormCreate event handler. So, what is needed to get the data into the grid, now the sorting functions : First, sorting of column 0 must be disabled since this column contains the images only. Therefore, the OnCanSort event handler is used and is :

procedure TForm1.AdvStringGrid1CanSort(Sender: TObject; aCol: Integer; var Boolean); 
begin
  dosort := acol > 0;
end;
Next, the datatype for sorting the other columns is queried with the OnGetFormat event handler. Column 1 and Column 3 can be sorted alphabetically (with and without case sensitivity) using the ssAlphabetic or ssAlphaNoCase sorting styles. For the second column, it can be choosen to display filesize in bytes or in mixed bytes/Kb style. When the filesize is displayed in bytes, the ssNumeric style can be choosen. When the filesize is displayed in mixed bytes/Kb size, the suffix parameter cannot be used here, since Kb means the numeric value must be multiplied by 1024. The prefix/suffix can be used for correctly sorting data like $1,$12,$120 or 1pk,12pk,120pk where prefix would be$ or suffix would be pk. But here in this case, there is no proper sorting style, so we must resort to the ssCustom style. How to do the custom sorting is implemented is discussed further. This is the OnGetFormat event handler, where a case structure is the easiest way to set formatting of all columns.

procedure
 TForm1.AdvStringGrid1GetFormat(Sender:
TObject; ACol: Integer;
var
 AStyle: TSortStyle; 
var
aPrefix, aSuffix: String); 
begin

  case
 acol 
of
  1,3: 
    if
 checkbox2.Checked 
then
 
      astyle:=ssAlphabetic 
    else
      astyle:=ssAlphanocase;
  
2:
    if
not
checkbox1.Checked 
then
      astyle:=ssCustom 
    else
      astyle:=ssNumeric;
  
4:astyle := ssDate;
  
end;
end;

When the ssCustom style is specified, the OnCustomCompare event handler is called with the cell data of the cells that it is comparing and here you must insert your own compare method. The result of the compare is specified in the res parameter and is 1 of str1>str2, -1 when str1<str2 and 0 when str1=str2. In this event handler, code is written to check for the Kb suffix and multiply by 1024 if so.

procedure TForm1.AdvStringGrid1CustomCompare(Sender: TObject; str1, str2: String; var res: Integer); 
var
  i1,i2,code:integer;
begin
  if (pos('Kb',str1)>0) then
  begin
    delete(str1,pos('Kb',str1),2);
    val(str1,i1,code);
    i1:=i1*1024;
  end

  else
 
    val(str1,i1,code);

  if (pos('Kb',str2)>0) then
  begin

    delete(str2,pos('Kb',str2),2);
    val(str2,i2,code);
    i2:=i2*1024;
  end

  else
    val(str2,i2,code);

  if (i1=i2) then
    res:=0
  else
  begin
    if (i1>i2) then
      res:=1
    else 
      res:=-1;
    end;
end;

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