Recent

Author Topic: How to access data in a TStringGrid cell ?  (Read 4744 times)

DenPit5

  • New Member
  • *
  • Posts: 21
How to access data in a TStringGrid cell ?
« on: March 15, 2019, 09:29:00 pm »
Can anyone show me the instructions to perform the above. Thank you.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: How to access data in a TStringGrid cell ?
« Reply #1 on: March 15, 2019, 09:38:34 pm »
Well I guess it depends if you are using columns property ?

In any case, use the MyStringGrid.CELLS[ X,Y ] = A_String_Of_Whats_There;

all data represented in the cells are strings, unless of course you are using the optional OBJECT store !


The only true wisdom is knowing you know nothing

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How to access data in a TStringGrid cell ?
« Reply #2 on: March 15, 2019, 10:24:48 pm »
An example is attached.
Data associated with a cell is shown in a hint when your mouse pauses over a cell.

DenPit5

  • New Member
  • *
  • Posts: 21
Re: How to access data in a TStringGrid cell ?
« Reply #3 on: March 16, 2019, 11:52:26 am »
Many thanks Howard & Jamie for your replies.   I was in fact aware of this method but what I actually want to be able to do (but didn't explain in my posting) is to visually be able to  select a cell Perhaps by clicking on it or some other way , then be able to extract the data from that Cell.    Is this Possible ? if so How ??

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How to access data in a TStringGrid cell ?
« Reply #4 on: March 16, 2019, 02:11:51 pm »
Not sure if you mean the string data or the Objects data associated with each cell.
In the example I gave you could add an OnSelection event something like the following. Make sure you don't have goRangeSelect in your grid Options.
Code: Pascal  [Select][+][-]
  1. uses  ..., Dialogs, ...;
  2.  
  3. procedure TForm1.gridSelection(Sender: TObject; aCol, aRow: Integer);
  4. var
  5.   grd: TStringGrid;
  6.   data: TData;
  7. begin
  8.   grd := Sender as TStringGrid;
  9.   data := grd.Objects[aCol, aRow] as TData;
  10.   ShowMessageFmt('string data: "%s", object data: %s',[grd.Cells[aCol, aRow], DateToStr(data.date)]);
  11. end;
  12.  

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: How to access data in a TStringGrid cell ?
« Reply #5 on: March 16, 2019, 04:48:40 pm »
When you move the selection to a cell the COL and ROW property points to that cell.

So you can use what ever event you want that triggers when selecting the cell then do this..

 With MyStringGrid do MyString := Cells[ COL,ROW ];

The only true wisdom is knowing you know nothing

nouzi

  • Sr. Member
  • ****
  • Posts: 296
My English is  bad
Lazarus last version free pascal last version
Lazarus trunk  free pascal trunk 
System : Linux mint  64bit  Windows 7 64bit

DenPit5

  • New Member
  • *
  • Posts: 21
Re: How to access data in a TStringGrid cell ?
« Reply #7 on: March 18, 2019, 12:39:27 pm »
Once again Many Thanks Howard & Jamie for the info you have posted.  I understand what you are saying but what   I want to be able to do is to visually scan the data in the Grid and when I have found the data I require then to be able to select the required cell in some way so that an event occurs that I have set a response for.    It is  HOW to select the cell visually that I need.??

Regards Dennis

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How to access data in a TStringGrid cell ?
« Reply #8 on: March 18, 2019, 07:18:06 pm »
It is  HOW to select the cell visually that I need.
The use selects cells/rows by moving the cursor using mouse (or arrow keys or Tab/Enter) and/or by clicking on a cell.
Visual feedback of selection state is controlled by the grid's Options property.
Particularly by including or excluding the values goRowSelect, goDrawFocusSelection, goSelectionActive, goRelaxedRowSelect.
Or are you asking about how to select a particular cell programmatically?

jesusr

  • Sr. Member
  • ****
  • Posts: 484
Re: How to access data in a TStringGrid cell ?
« Reply #9 on: March 19, 2019, 02:12:43 am »
Once again Many Thanks Howard & Jamie for the info you have posted.  I understand what you are saying but what   I want to be able to do is to visually scan the data in the Grid and when I have found the data I require then to be able to select the required cell in some way so that an event occurs that I have set a response for.    It is  HOW to select the cell visually that I need.??

Regards Dennis

grid.Col := 3;
grid.Row := 4;

This select the cell 3,4

 

TinyPortal © 2005-2018