2

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'?

Rynardald
  • 329
  • 2
  • 10
  • This is just the nature of grids, there's always something selected at any given time. Same in VCL. – Jerry Dodge Jul 21 '15 at 14:27
  • Not at any time. In the time between creating and first cell-selecting the property 'selected' is '-1'. – Rynardald Jul 21 '15 at 15:00
  • Well yeah, before anything has been added, there's nothing to select. Once a row has been added, it defaults to 0. Now if the same procedure which adds the row also checks the index, it may temporarily be -1 until after the procedure exits. – Jerry Dodge Jul 21 '15 at 15:36
  • is there a way to check if a row is highlighted and which row is highlighted? Because the highlighting could be removed with 'grid.selectRow(-1)'. – Rynardald Jul 21 '15 at 16:40
  • What are you actually trying to do ? We are just guessing. For instance you can defocus the grid. But the answer depends on what your goal is !!! – Rohit Gupta Jul 21 '15 at 20:34
  • My intention was to check with `grid.selected = -1` if the user has some row selected or not. But `grid.selected` is set automaticly to '0' if its smaller than '0', so its useless for me. – Rynardald Jul 22 '15 at 07:18
  • You may just be stuck with this - a dataset will always have a pointer to record, a grid is just a graphical representation of this. Forcing an internal property to be something the designers have prevented you from setting could have odd consequences if you're not careful (and if not now, then when you upgrade the component). You may just need to have a separate checkbox/button to 'deselect' the grid. You could package this up in a frame/new control if needed. – Matt Allwood Jul 22 '15 at 08:22

2 Answers2

1

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;
Zam
  • 2,880
  • 1
  • 18
  • 33
-2

If your aim is to not show the selected row in the grid then you can just defocus it by focusing on another component.

Rohit Gupta
  • 4,022
  • 20
  • 31
  • 41