Board index » delphi » DBgrid and derived fields

DBgrid and derived fields

  Hi, I'm having a little problem with dbgrid. I can read in the
values from my table into the dbgrid fine.  

  However most of the data in the table is stored as single characters
which represent words, ie an 'A' in the status field indicates
'Active' etc.  What I want to do is have my DBgrid display 'Active'
instead of 'A'.

  Can anybody tell me how to do this, or where to find a component
that can do this.

Thanks in advance, Rob.

P.S.  TStringGrid is out of the question due to the number of records
and speed issues.

 

Re:DBgrid and derived fields


Hi Rob (brilliant name...)

first thing that is important is are those status values saved
in a table or not. If not, you could create a calculated string
field for example 'statusdes', and in the oncalculate event of the
table you do something like :

if statusfield.value = 'A' then statusdes.value = 'Active'
if statusfield.value = 'N' then statusdes.value = 'Non-active' etc...

in the grid you don't display the statusfield but only the
calculated field statusdes.

Greetings,

Rob Segerink

Quote
Rob Morgan wrote in message <3946ffc9.5074977@postoffice>...
>  Hi, I'm having a little problem with dbgrid. I can read in the
>values from my table into the dbgrid fine.

>  However most of the data in the table is stored as single characters
>which represent words, ie an 'A' in the status field indicates
>'Active' etc.  What I want to do is have my DBgrid display 'Active'
>instead of 'A'.

>  Can anybody tell me how to do this, or where to find a component
>that can do this.

>Thanks in advance, Rob.

>P.S.  TStringGrid is out of the question due to the number of records
>and speed issues.

Re:DBgrid and derived fields


Use the OnGetText and OnSetText event handlers of the field, e.g.:

procedure TForm1.Table1Field1GetText(Sender: TField; var Text: String;
  DisplayText: Boolean);
begin
  if Sender.IsNull then
    Text := ''
  else
  if Sender.AsString = 'A' then
    Text := 'Active'
  else
    Text := 'Inactive';
end;

procedure TForm1.Table1Field1SetText(Sender: TField; const Text: String);
begin
  if Text = 'Active' then
    Sender.AsString := 'A'
  else
  if Text = 'Inactive' then
    Sender.AsString := 'I'
  else
    Sender.Clear;
end;

ps. I also like to setup a picklist containing the possible field values for
the DBGrid column.

"Rob Morgan" <rmor...@smf.com.au> schreef in bericht
news:3946ffc9.5074977@postoffice...

Quote
>   Hi, I'm having a little problem with dbgrid. I can read in the
> values from my table into the dbgrid fine.

>   However most of the data in the table is stored as single characters
> which represent words, ie an 'A' in the status field indicates
> 'Active' etc.  What I want to do is have my DBgrid display 'Active'
> instead of 'A'.

>   Can anybody tell me how to do this, or where to find a component
> that can do this.

> Thanks in advance, Rob.

> P.S.  TStringGrid is out of the question due to the number of records
> and speed issues.

Other Threads