3

Is there anyway to determine which rows are currently selected in a multi-select TStringGrid, or a TCustomGrid for that matter. A property would be ideal.

I know that there is the gdSelected property that gets set in the DrawCell event,

procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState);

I can check AState for gdSelected, and keep track of this in an array somewhere, but this seems kludgey.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
sse
  • 987
  • 1
  • 11
  • 30

3 Answers3

4

I guess you are talking about a range-select string grid, that is, a string grid with goRangeSelect in Options. Then you can use the Selection property. This is (essentially) a TRect in which you can find the upper-left and lower-right cell in the range selection.

Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
  • 1
    I don't think this will work, the selected rows are not necessarily contiguous. – sse May 10 '11 at 15:33
  • 2
    @user746911: No, I assumed that there is no 'extended select' (as in Ctrl-clicking individual cells). It seems to me, however, that the `TStringGrid` does not support extended select. (If it does -- how in the world do you enable it?!) – Andreas Rejbrand May 10 '11 at 15:35
  • AFAICT, @Andreas is right. The standard `TStringGrid` doesn't support non-contiguous selections. `TDBGrid` does, however, by keeping a list of bookmarks of selected rows (`TDBGrid.SelectedRows` property). – Ken White May 10 '11 at 22:13
  • Ah, I see, you are both right, thanks, TStringGrid.selection.bottom and TStringGrid.selection.top work perfectly for this. Thank you again. – sse May 11 '11 at 01:18
1

Oooh, I'm use StringGrid.Selection.BottomRight.Y to determine rows and StringGrid.Selection.BottomRight.X for columns.

Marijn
  • 10,367
  • 5
  • 59
  • 80
Yuriy
  • 11
  • 2
0

Use string grid selection property StringGrid.Selection.top will give you the top selected row, StringGrid.Selection.bottomwill give you the end selected row, where the selection stops.

Example:

If you select from row 3 to row 6 in a string grid thenStringGrid.Selection.top will give you 3 as an output value StringGrid.Selection.bottom will give you 6 as an output value, and remaining values you can get by iterating from the top to bottom.

celroy
  • 11
  • 3