Contents
- Index
- Previous
- Next
OnCompareCells event
Applies to
TProfGrid component
Declaration
property OnCompareCells: TProfGridCompareCellsEvent;
Description
Provides a way for custom sorting of certain cells.
Result is the parameter that indicates how the cells are to be ordered. Result must be set to a value < 0 if Cell1 is less than Cell2, 0 if they are equal and > 0 if Cell1 is greater than Cell2.
An application can use the OnCompareCells event to implement custom sorting of any level of complexity, thus overriding the built-in sorting algorithm.
Tip: The grid never changes the order of rows that contain equal values. Thus, if an application-provided OnCompareCells event handler returns 0 in the Result parameter, the order of two rows is guaranteed to be unchanged.
Examples
The following event handler does not allow the bottom row to be included in the sort.
procedure TForm1.ProfGrid1CompareCells(Sender: TProfGrid; Cell1,
Cell2: TProfGridCell; var Result: Integer);
begin
if (Cell1.EndRow = Sender.RowCount - 1) or (Cell2.EndRow = Sender.RowCount - 1) then
Result := 0
else
Result := Sender.DefaultCompareCells(Cell1, Cell2)
end;
The following event handler sorts on more than one column. E.g.: You have a grid with ages and names:
18 Cliff
18 Andy
23 Bill
21 Peter
You want to sort them like this (by ages and then by names):
18 Andy
18 Cliff
21 Peter
23 Bill
procedure TForm1.ProfGrid1CompareCells(Sender: TProfGrid; Cell1,
Cell2: TProfGridCell; var Result: Integer);
const
ages_column = 1;
names_column = 2;
begin
Result := Sender.DefaultCompareCells(Cell1, Cell2);
if Sender.SortColumn = ages_column then
if Result = 0 then
Result := Sender.DefaultCompareCells(
Sender.Cells[names_column, Cell1.StartRow],
Sender.Cells[names_column, Cell2.StartRow] )
end;