TAdvStringGrid
Example 6 : Imagelist images + 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;
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;
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;
×