Bookstore

Recent

Author Topic: How to read cell in TStringGrid?  (Read 325 times)

VinDerKlaus

  • New Member
  • *
  • Posts: 27
How to read cell in TStringGrid?
« 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.

wp

  • Hero Member
  • *****
  • Posts: 6830
Re: How to read cell in TStringGrid?
« Reply #1 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"?
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

VinDerKlaus

  • New Member
  • *
  • Posts: 27
Re: How to read cell in TStringGrid?
« Reply #2 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.

winni

  • Hero Member
  • *****
  • Posts: 1114
Re: How to read cell in TStringGrid?
« Reply #3 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





lucamar

  • Hero Member
  • *****
  • Posts: 2579
Re: How to read cell in TStringGrid?
« Reply #4 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];
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.6/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

VinDerKlaus

  • New Member
  • *
  • Posts: 27
Re: How to read cell in TStringGrid?
« Reply #5 on: February 27, 2020, 08:06:32 pm »
Thank you lucamar, work great!!