Selecting a row in TStringGrid from click on any cell within that row.
Asked Answered
R

2

7

I have a TStringGrid object on a form which has 1 FixedCol and 1 FixedRow. I want to be able to select an entire row on the object when the user clicks on any cell within that row. This selection must also be visible to the user (I want the row to change colour).

EDIT: Have put goRowSelect in options on the object. Is there now a way to select the row on the click of one of the cells in the fixed column?

Reconstructionist answered 28/2, 2014 at 11:50 Comment(0)
P
5

If you enable the option goFixedColClick of the string grid you can then use the OnFixedCellClick event to determine which row to select. You can then set StringGrid1.Selection := TGridRect(Rect(0, Row, n, Row)); where Row is the row clicked and n is the width of your StringGrid.

Pehlevi answered 28/2, 2014 at 12:4 Comment(3)
Really sorry for my ignorance here... But how would I go about selecting the Row? I understand that I will need to use the ARow and ACol variables declared in the Sender section of the procedure but other than that, am pretty lost.Reconstructionist
@Reconstructionist I've updated my answer to include details on how to select a row in code.Pehlevi
Don't forget to make sure that the user clicked on a non-fixed row before changing the selection.Categorical
S
7

In order highlight entire row when ever the user clicks on any cell in that row, set the following StringGrid properties.

In design time: Go to options property and check the following sub-properties.

goEditing := TRUE;
goRowSelect := TRUE;

To achieve this at run time,

StringGrid1.Options := StringGrid1.Options + [goEditing];
StringGrid1.Options := StringGrid1.Options + [goRowSelect];
Swampland answered 1/3, 2014 at 10:5 Comment(0)
P
5

If you enable the option goFixedColClick of the string grid you can then use the OnFixedCellClick event to determine which row to select. You can then set StringGrid1.Selection := TGridRect(Rect(0, Row, n, Row)); where Row is the row clicked and n is the width of your StringGrid.

Pehlevi answered 28/2, 2014 at 12:4 Comment(3)
Really sorry for my ignorance here... But how would I go about selecting the Row? I understand that I will need to use the ARow and ACol variables declared in the Sender section of the procedure but other than that, am pretty lost.Reconstructionist
@Reconstructionist I've updated my answer to include details on how to select a row in code.Pehlevi
Don't forget to make sure that the user clicked on a non-fixed row before changing the selection.Categorical

© 2022 - 2024 — McMap. All rights reserved.