TAdvStringGrid - cell selection issue

Hi, I have problems with TAdvStringGrid cell selection. Selected cells are reported as not selected and counted incorrectly


How to reproduce:
put a TAdvStringGrid on a form and use following settings different from default (not sure if all of them does matter, but that is my setup):
goRangeSelect:= false
goEditing:= true
goTabs:= true
AllowCtrlEnter:= false
AlwaysEdit:= true

put two edits on the form and use following onSelectCell action:

procedure TForm1.AdvStringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
  var CanSelect: Boolean);
begin
  edit1.Text:= inttostr(AdvStringGrid1.SelectedCellsCount);
  edit2.Text:= 'cell ' + inttostr(ACol) + ',' + inttostr(ARow) + ', selected = ' + IfThen(AdvStringGrid1.SelectedCells[ACol,ARow],'True','False');
end;

1. run program
2. click on cells - edit1 shows 0 as 0 selected cells. Is that OK? edit2 shows selected = false. Is that OK?
3. hold ctrl and keep selecting different cells. edit1 shows amount of really selected cells minus one all the time. As you add more cells into the selection, the selected = false in edit2 does not change at all.
4. hold ctrl and click on already selected cell, which should deselect it - edit1 amount is incremented and edit2 shows selected = true! But the cell has been deselected.
5. click anywhere else and the values are wrong again.

Please tell me if I am doing something wrong, or there is a bug. For my program, I need to get reliable informations about selected cells and be able to manipulate with the selection based on information that specific cell is selected or not right after the selection/deselection has been made. Thank you

I forgot to mention one important setting of the grid


DisjunctCellSelect:= true

The OnSelectCell event is triggered in the process of cell selection before final selection is made (as it offers the capability to disable the selection by setting CanSelect = false), so it is not a good idea to do checks on SelectedCellsCount / SelectedCell from this event.


A better place is the OnSelectionChanged event. This sample code applied to a default grid shows how you track the disjunct selected cells using this event:

procedure TForm1.AdvStringGrid1SelectionChanged(Sender: TObject; ALeft, ATop,
  ARight, ABottom: Integer);
var
  s: string;
  i: integer;
begin
  caption := inttostr(advstringgrid1.SelectedCellsCount);

  listbox1.Items.Clear;

  for i := 0 to advstringgrid1.SelectedCellsCount - 1 do
    begin
      s := inttostr(advstringgrid1.SelectedCell.x)+':'+inttostr(advstringgrid1.SelectedCell.y);
      listbox1.items.Add(s);
    end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  advstringgrid1.Options := advstringgrid1.Options + [goEditing, goTabs] - [goRangeSelect];
  advstringgrid1.Navigation.AllowCtrlEnter := false;
  advstringgrid1.Navigation.AlwaysEdit := true;
  advstringgrid1.MouseActions.DisjunctCellSelect := true;
end;


Great news, thank you very much for explanation.


Still, I have one question - still in the OnSelectCell event - I found out, that is SelectedCellsCount = 0, when a single cell is being selected, with DisjunctCellSelect:= false, and is SelectedCellsCount = 1, when a single cell is being selected with DisjunctCellSelect:= true. Why?

SelectedCellsCount / SelectedCell only make sense and are only used for disjunct cell selection. When no disjunct cell selection is used, selected cell is indicated by grid.Col, grid.Row or a range of selected cells indicated by grid.Selection: TGridRect 

OK, thank you