TMSFNCDataGrid sorting with CustomCompare

I use the logic from the Data Grid Reference:

// Custom comparison logic for sorting
  if Grid.Floats[ACol, ARow1] > Grid.Floats[ACol, ARow2] then
    AResult := 1
  else if Grid.Floats[ACol, ARow1] < Grid.Floats[ACol, ARow2] then
    AResult := -1
  else
    AResult := 0;

It throws an exception when Result:=-1

Here my code:

function TForm.GridCustomCompare(Sender: TObject; AColumn, ARow1, ARow2: Integer; AData1, AData2: TTMSFNCDataGridCellValue; ALevel: Integer): Integer;
begin
  // Custom comparison logic for sorting
  if Grid.Ints[AColumn, ARow1] > Grid.Ints[AColumn, ARow2] then
    Result := 1
  else if Grid.Ints[AColumn, ARow1] < Grid.Ints[AColumn, ARow2] then
    Result := -1
  else
    Result := 0;
end;

By the way: The parameter list of the function is different to the one in the manual…

You need to use AData1, AData2 parameters.

function TForm1.TMSFNCDataGrid1CustomCompare(Sender: TObject; AColumn, ARow1,
  ARow2: Integer; AData1, AData2: TTMSFNCDataGridCellValue;
  ALevel: Integer): Integer;
begin
  // Custom comparison logic for sorting
  if TMSFNCDataGrid1.ValueToInteger(AData1) > TMSFNCDataGrid1.ValueToInteger(AData2) then
    Result := 1
  else if TMSFNCDataGrid1.ValueToInteger(AData1) < TMSFNCDataGrid1.ValueToInteger(AData2) then
    Result := -1
  else
    Result := 0;
end;

Thanks a lot, works out fine…

Is it possible to redirect a sort click on a header to another column?

I need to sort a hidden column instead of the one that has been cklicked.

Sure this is possible.

procedure TForm1.TMSFNCDataGrid1BeforeSortColumn(Sender: TObject;
  AColumn: Integer; var ACanSort: Boolean);
var
  sdx: TTMSFNCDataGridSortIndex;
  sd: TTMSFNCDataGridSortDirection;
begin
  ACanSort := False;
  sd := gsdAscending;
  if TMSFNCDataGrid1.SortIndexList.Find(1, sdx) then
  begin
    case sdx.Direction of
      gsdAscending: sd := gsdDescending;
      gsdDescending: sd := gsdAscending;
    end;
  end;

  TMSFNCDataGrid1.Sort(1, sd);
end;