Search code examples
delphifiremonkeytgrid

How to set 'selected' property of TGrid to -1


I am using a FMX.Grid.TGrid in which a user can select complete rows. In some cases I want to reset this selection. If I do this with grid.selected = -1 or with grid.selectRow(-1) the selection is removed from the grid but grid.selected is set to '0' (in TCustomGrid.SelectCell), which is the first row.

How can i reset the selection so that the property grid.selected is '-1'?


Solution

  • I checked code of FMX library I did tiny class helper, which allow you to get direct access to private property which are store value of selected row. Tested on Delphi XE8. This code will work correctly, even if you have options "AlwaysShowSelection" enabled.

      TMyG = class helper for TCustomGrid
      public
        procedure DoSomethingStrange;
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.fmx}
    
    procedure TForm1.btnReadSelectionClick(Sender: TObject);
    begin
      Caption := Grid1.Selected.ToString;
    end;
    
    procedure TForm1.btnResetSelectionClick(Sender: TObject);
    begin
      Grid1.DoSomethingStrange;
    end;
    
    { TMyG }
    
    procedure TMyG.DoSomethingStrange;
    begin
      Self.FSelected := -1;
      Self.UpdateSelection;
    end;