Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

DBGRID - Change the display of a Field

1,956 views
Skip to first unread message

Carlos Moreira

unread,
Apr 8, 2003, 6:15:30 PM4/8/03
to
I´m using a dbgrid that has a integer field.
When i display the dbgrid, i need change this value to a string text,
transformed by a function.
How can i do this, using the OnDrawColumnCell Event.
Thanks.

Richard Gilbert

unread,
Apr 8, 2003, 6:35:28 PM4/8/03
to
In the OnDrawColumnCell, you can use a variety of methods to determine
if it is the correct column and convert its value. I didn't know what
you wanted to do with the converted integer so I just stuck it in a
string variable. Is this what you needed?

//'SOME_INTEGER_FIELD' is the database field name for the
//desired column.
if Column.FieldName = 'SOME_INTEGER_FIELD' then
aString := IntToStr(Column.Field.AsInteger);

Carlos Moreira

unread,
Apr 8, 2003, 7:01:24 PM4/8/03
to
Yes and thanks.
I wrote the following code:

if (Column.FieldName = 'ID_GROUP') then
begin
dbgrPERFIL.Canvas.FillRect(Rect);
dbgrPERFIL.Canvas.TextOut(Rect.Left, Rect.Top,
RETURNGROUP(Column.Field.AsInteger));
end
else
begin
dbgrPERFIL.DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;

"Richard Gilbert" <rag...@bellsouth.net> escreveu na mensagem
news:3E934EB0...@bellsouth.net...

Constantine Yannakopoulos

unread,
Apr 9, 2003, 5:19:04 AM4/9/03
to
Carlos Moreira wrote:

> I4m using a dbgrid that has a integer field.

It may be a better idea to use TField.OnGetText and OnSetText and do
the transformation (and its reverse) there. This way the field could be
made editable too. If you use OnDrawColumnCell, when the user invokes
the grid's editor for that field the integer value will be displayed
instead of the transformed string.

Chris Luck

unread,
Apr 9, 2003, 8:00:06 AM4/9/03
to
"Constantine Yannakopoulos" <ky...@deltasingular.remove.this.gr> wrote in
message news:3e93...@newsgroups.borland.com...

I tend to agree with that, though there are occasions when you might want the
display in a grid to be different from the display of the same field in an
edit, for instance, in which case the Set/GetText approach becomes too
constraining. Suppose you want to display a boolean in a grid as a checkmark
and as True/False in an Edit, here the OnDrawColumnCell is useful. To
overcome the InplaceEditor popping-up when editing the checkmark the Grid's
ReadOnly property can be set in the OnColumnEnter event leaving you free to
draw as you wish.


--
Regards,
Chris Luck.


Robert Cerny

unread,
Apr 9, 2003, 8:10:22 AM4/9/03
to
"Constantine Yannakopoulos" <ky...@deltasingular.remove.this.gr> wrote in
message news:3e93...@newsgroups.borland.com...
>
> It may be a better idea to use TField.OnGetText and OnSetText and do
> the transformation (and its reverse) there. This way the field could be
> made editable too. If you use OnDrawColumnCell, when the user invokes
> the grid's editor for that field the integer value will be displayed
> instead of the transformed string.
>
>
Agreed. And it's not just that. If you need another renderer of the field
(an editbox, on another form, on report,...), you have to redo everything
you do in OnDrawColumnCell.

--
Robert Cerny
http://codecentral.borland.com/codecentral/ccWeb.exe/author?authorid=18355


Constantine Yannakopoulos

unread,
Apr 9, 2003, 10:19:42 AM4/9/03
to
Chris Luck wrote:

> I tend to agree with that, though there are occasions when you might
> want the display in a grid to be different from the display of the
> same field in an edit,

Yes.

> for instance, in which case the Set/GetText
> approach becomes too constraining. Suppose you want to display a
> boolean in a grid as a checkmark and as True/False in an Edit, here
> the OnDrawColumnCell is useful.

In this case creating a TDBGrid descendant that displays checkboxes for
boolean fields would be worth the effort.

> To overcome the InplaceEditor
> popping-up when editing the checkmark the Grid's ReadOnly property
> can be set in the OnColumnEnter event leaving you free to draw as you
> wish.

That's one way of doing it. The other (if you do implement a
descendant) is to override CanEditShow (IIRC) and return False for
boolean fields.

Chris Luck

unread,
Apr 9, 2003, 10:08:49 AM4/9/03
to
"Constantine Yannakopoulos" <ky...@deltasingular.remove.this.gr> wrote in
message news:3e94...@newsgroups.borland.com...

> (if you do implement a descendant)

I'll get around to it one of these days. :)


--
Regards,
Chris Luck.

0 new messages