Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2014-09

How to associate an object with a row in TStringGrid

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Dennis

when displaying data in a TStringGrid, I want to associate an object with each row of the grid so that when later use select a certain row, I can just determine the selected row and then retrieve the associate object for further processing (e.g. display further information of this object in some controls).

How do I do that?

Currently, I use OnSelection (Sender: TObject; aCol, aRow: Integer) to obtain aRow but how do I associate an object with each row?

Thanks.

Dennis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140903/af3abbfe/attachment.html>

Reply by email

Giuliano Colla
to Dennis

Il 03/09/2014 10:17, Dennis ha scritto:

Show quoted text 11 lines
when displaying data in a TStringGrid, I want to associate an object with each row of the grid so that when later use select a certain row, I can just determine the selected row and then retrieve the associate object for further processing (e.g. display further information of this object in some controls). How do I do that? Currently, I use OnSelection (Sender: TObject; aCol, aRow: Integer) to obtain aRow but how do I associate an object with each row?

Example:

StringGrid1.Rows[0].AddObject('some text',anObject);

Giuliano
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140903/05f3b22d/attachment.html>

Reply by email

Dennis
to Giuliano Colla
Giuliano Colla wrote 19 lines
Il 03/09/2014 10:17, Dennis ha scritto: > when displaying data in a TStringGrid, I want to associate an object > with each row of the grid so that when later use select a certain > row, I can just determine the selected row and then retrieve the > associate object for further processing (e.g. display further > information of this object in some controls). > > How do I do that? > > Currently, I use OnSelection (Sender: TObject; aCol, aRow: Integer) > to obtain aRow but how do I associate an object with each row? > Example: StringGrid1.Rows[0].AddObject('some text',anObject); Giuliano

Thanks.

But I checked,
StringGrid1.Rows[0] is a TStrings

this line

    StringGrid1.Rows[0].AddObject('some text',anObject);
  seems to add an object to the last item in this TStrings instead of

just one object for the entire row.

Dennis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140904/0ea96e79/attachment.html>

Reply by email

Giuliano Colla
to Dennis

Il 03/09/2014 18:03, Dennis ha scritto:

Show quoted text 32 lines
Giuliano Colla wrote: > > Il 03/09/2014 10:17, Dennis ha scritto: >> when displaying data in a TStringGrid, I want to associate an object >> with each row of the grid so that when later use select a certain >> row, I can just determine the selected row and then retrieve the >> associate object for further processing (e.g. display further >> information of this object in some controls). >> >> How do I do that? >> >> Currently, I use OnSelection (Sender: TObject; aCol, aRow: Integer) >> to obtain aRow but how do I associate an object with each row? >> > > Example: > > StringGrid1.Rows[0].AddObject('some text',anObject); > > Giuliano Thanks. But I checked, StringGrid1.Rows[0] is a TStrings this line StringGrid1.Rows[0].AddObject('some text',anObject); seems to add an object to the last item in this TStrings instead of just one object for the entire row.

Yes, actually you can associate an object to each Cell, you cannot associate an object to each Row.
If you need just an Object per row, you must decide in which column to put it. e.g. you may decide that your object will always be in column 0. sort of:

   anObject := TObject.Create;
   StringGrid1.Cells[0,0] := 'Some Row Caption';
   StringGrid1.Objects[0,0] := anObject;

Then in your OnSelection, you pick up the Row, but you look only at the object in column 0.
Sort of:

procedure TForm1.StringGrid1Selection(Sender: TObject; aCol, aRow: Integer);
   var
     myObject: TObject;
begin
   if Sender is TStringGrid then begin
     myObject := StringGrid1.Objects[0,aRow];
   end;
end;

Giuliano

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140903/4c8f71d7/attachment.html>

Reply by email