BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

StringGrid Cell Formating

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Students)
View previous topic :: View next topic  
Author Message
Roger
Guest





PostPosted: Mon Mar 19, 2007 3:35 am    Post subject: StringGrid Cell Formating Reply with quote



I would like to format StringGrid Cells based on the contents of cells
within a row. In TListView I would use a data structure associated with
ListItem->Data.

Is there a similar, or recommended, method of doing this with StringGrids?

Roger
Back to top
JD
Guest





PostPosted: Mon Mar 19, 2007 8:10 am    Post subject: Re: StringGrid Cell Formating Reply with quote



Roger <aretae (AT) magma (DOT) ca> wrote:
Quote:

I would like to format StringGrid Cells based on the
contents of cells within a row. In TListView I would use a
data structure associated with ListItem->Data.

Is there a similar, or recommended, method of doing this
with StringGrids?

A similar approach would be to use the Objects property
associated with each Cell. To implement it, set the Grid's
DefaultDrawing property to false and add an OnDrawCell event
where you would use the ACol and ARow parameters to know
which Objects[][] to use to format the Cell[][].

The Objects array is an array of TObject*'s which is nothing
more that a 32bit variable. You can use it to store actual
data that only you know what it means or you can use it to
store a pointer to data that needs more than 32bits - your
choice.

The only 'trick' is that you have to cast the data in and out
of the Objects property. For example:

struct TMyDataRecord
{
private:
public:
TMyDataRecord()
{
// add initialization for the struct here
}
~TMyDataRecord()
{
// add destruction code (if any) here
}
};

// allocates directly into the Objects property
// requires that you iterate Objects in the form's dtor
// to destroy the items in the Objects array
Objects[Col][Row] = reinterpret_cast<TObject*>( new TMyDataRecord() );

// to put it in after allocation
Objects[ACol][ARow] = reinterpret_cast<TObject*>( SomeRecordPointer );

// to get it out
TMyDataRecord *Record = reinterpret_cast<TMyDataRecord*>( Grid->Objects[ACol][ARow] );

// to directly destroy it
delete reinterpret_cast<TMyDataRecord*>( Grid->Objects[ACol][ARow] );

The truth is that you could implement a 2D array that matches
the grid's Col's and Row's and get the same effect but the
Objects array is there for you to use as you see fit and it's
managed by the grid as a dynamic array so that would be my
choice. The difference with a TStringGrid and a TListView is
that you don't get any notification when a certain Item is
being removed so you need to be careful not to leak memory.

~ JD
Back to top
Roger
Guest





PostPosted: Mon Mar 19, 2007 8:19 pm    Post subject: Re: StringGrid Cell Formating Reply with quote



Thanks JD, that is exactly what I was looking for.

Roger
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Students) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.