Force position in a TStringGrid

After having filled in a TStringGrid programmatically, I want to force a specific row, say row N, to be "selected"
How can I achieve that
LVL 1
LeTayAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jimyXCommented:
Make sure to set the following options of the StringGrid from the OI:
goRangeSelect = False
goRowSelect = True

Use OnSelectCell event to test for the current select row if it matches your target row then allow select, otherwise CanSelect equals False:
//Set the current selected row:
procedure TForm1.FormCreate(Sender: TObject);
begin
  StringGrid1.Row:= N;
end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
begin
  CanSelect:= False;

  //list the allowed rows to be selected
  if ARow = N then
    CanSelect:= True;
end;

Open in new window

0
LeTayAuthor Commented:
Ok
If now I use StringGrid1.Row = something somewhere in my code, does that forces the selectcell event automatically ?
0
jimyXCommented:
As you can see, at OnSelectCell event there is "N", where the comparison takes place to either allow or deny selecting rows, so it all depends on what you assign to this "N".
So no, it will not be automatically, you need to assign value to the "N" variable every time you need to update the selected row(s).

If you need other solution, it would be disable the entire StringGrid after selecting the row, unless you need to do some work on it, in this case you need to restrict selection by OnSelectCell event.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

LeTayAuthor Commented:
Well, not sure to have explained exactly what I needed.
When the form is displayed, the TStringGrid is filled in (from database data).
The "selected" row is the first one below the titles.
User click on another row, that row becomes the selected one.
At that time, the user can do some action, and after that action, I refresh completely the TStringGrid (clear and filled in again)
How can I then, at that precise time, have that previously select row (says row N), become the selected (and highlighted) one ?
0
jimyXCommented:
> Well, not sure to have explained exactly what I needed.

Now it is clear.
Then all you have to do is:
N:= StringGrid1.Row;
...do update the StringGrid
StringGrid1.Row:= N;

Open in new window


But this means your StringGrid will always be matching the records count/order before updates.
For instance, when updating (doing some actions, like deleting record) the rows count is different now and when filling the StringGrid there is a possibility that you might select the wrong row.
Hence you need to have a key that you store before updating and after the update start looking for that key and select the row that is matching.

Is it possible to use DBGrid instead of StringGrid? DBGrid will offer better utilization of DB linking and Locate functionality.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LeTayAuthor Commented:
Many thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.