Lazarus

Programming => General => Topic started by: VinDerKlaus on February 27, 2020, 06:59:55 pm

Title: How to read cell in TStringGrid?
Post by: VinDerKlaus on February 27, 2020, 06:59:55 pm
If I have stringgrid and I want to read second cell in a selected row and assign to a string variable, how do this?

Thank you.
Title: Re: How to read cell in TStringGrid?
Post by: wp on February 27, 2020, 07:19:24 pm
What is the "second" cell? A grid is two-dimensional. You mean "second cell in first row" or "second cell in third column"?
Title: Re: How to read cell in TStringGrid?
Post by: VinDerKlaus on February 27, 2020, 07:29:33 pm
Good day wp.

I mean whichever row I have selected (I have goRowHighlight enabled) second cell in that row (horizontal, second from left) will be read. Thank you wp.
Title: Re: How to read cell in TStringGrid?
Post by: winni on February 27, 2020, 07:38:24 pm
Hi

Connect the onSelectCell event of your stringGrid with the following procedure:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1SelectCell(Sender: TObject; aCol, aRow: Integer;
  2.                 var CanSelect: Boolean);
  3. var s : string;
  4. begin    
  5. s := StringGrid1.Cells[1,Arow];
  6. showMessage (s);
  7. end;

Winni




Title: Re: How to read cell in TStringGrid?
Post by: lucamar on February 27, 2020, 07:40:38 pm
Or just do something like:
Code: Pascal  [Select][+][-]
  1. if MyGrid.Row >= 0 then
  2.   Value := MyGrid.Cells[1, MyGrid.Row];
Title: Re: How to read cell in TStringGrid?
Post by: VinDerKlaus on February 27, 2020, 08:06:32 pm
Thank you lucamar, work great!!
TinyPortal © 2005-2018