How do I access TStringGrid object?

Hello.  I'm using a TStringGrid.  For each row, I need to display data from a database record.  For the most part, that is working fine.  I also need to store the record id in the grid, but not make it visible.  The record key is an integer.

So I store this value like this, for example:

var SomeRecordID : integer;

SomeRecordId:= 123456;

stringgrid1.rows[gridrow].AddObject('',Tobject(somerecordid));

When I try to reference that same object I just added, it returns a 0 because the object is not assigned.

showmessage(inttostr(integer(stringgrid1.Rows[gridrow].Objects[0])));  //objects[0] is nil yet I just added it!

So, I guess my question is, once I add an object, how do I read that same object?  I've done something similar using a TListview before, so I don't understand what makes this different.

Parents
  • Don't use AddObject(), just set the Object[] of an existing cell in the row, eg:

    StringGrid1.Rows[GridRow].Objects[0] := TObject(somerecordid);
    ...
    ShowMessage(IntToStr(Integer(StringGrid1.Rows[GridRow].Objects[0])));

    Which is really just a wrapper for the grid's own Objects[] property:

    StringGrid1.Objects[0, GridRow] := TObject(somerecordid);
    ...
    ShowMessage(IntToStr(Integer(StringGrid1.Objects[0, GridRow])));

Reply Children
No Data