Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to convert integer to string in DBGrid 1

Status
Not open for further replies.

krisleech

Programmer
Mar 15, 2003
6
GB
Hi ya all,

I have a DBGrid, one of the fields is an integer field.
Each integer repsents a stage or status. How do I make the DBGrid show a string instead of the integer.
eg. 1 would be replaced by "Start", 2 would be replaced by "Middle" and 3 replaced by "End".

Any help or pointers would be much appricated, Cheers Kris.
 
try implementing this login in the cell paint event...
 
Thanks would that be the DrawDataCell event of the DBgrid?
If so I have "const Rect: TRect; Field: TField;" availible to use, I could tell which field I am on using the Field varible however I cant refrence this witha string eg. If Field = 'status' then XYZ.... TField and String not compatible.

Cheers Kris.
 
Thanks, i tried:

if field.AsInteger = 0 then MessageDlg('ok', mtWarning, [mbOK], 0);

But no popup message is displayed.

How would you use this function is field.asinteger refering to the field number or the actual value of a field?

Cheers for the help, Kris.
 
Are the string values that define the numbers in a separate table? If you, you'll create a Lookup field by going to the fields editor and adding a lookup field that points to the correct table. If they're not, you can create a Calculated field and in the OnCalcFields event handler, use code like this:
Code:
Case MyTable.FieldByName('My_Number').AsInteger of
1: MyTable.FieldByName('My_Numbers_Value').AsString := 'Start';
2: MyTable.FieldByName('My_Numbers_Value').AsString := 'Middle';
3: MyTable.FieldByName('My_Numbers_Value').AsString := 'End';
end;

-D
 
> Are the string values that define the numbers in a separate table?

No, I want to use hard code to change the numbers in to string.

The code looks good, but I dont have a OnCalcField event!?!
Im using Delphi 5, standard Dbgrid component.
Do I need to create a calculated field?

Cheers Caz.
 
What type of dataset are you using? The OnCalcField event is on the dataset (TTable, etc.), not on the TDBGrid.

-D
 
Hi Kris,

Use persistent fields in the dataset feeding the TDBGrid.
In the OnGetText event put code like:

Code:
procedure TMyTableStageGetText(Sender: TField; var Text: String; DisplayText: Boolean);
begin
  case Sender.asInteger of
  1:Text:='Start';
  2:Text:='Middle';
  3:Text:='End';
  end;{case}
end;

If you want the field to be editable,
- use peresisten columns of the grid and populate the PickList of the relevant column.
- Populate the field OnSetText doing the reverse [if(Text='Start' then Sender.asInteger:=1, etc]

If you want to be flash create an enum of Start/Middle/Finish then use the function GetEnumName (in Typinfo.pas) instead of a case statement in the OnGetText event.

It can also be done, neatly, using a lookup field, but I think the above will suit your purpose.

Have fun
Simon
 
Thanks VintageWine (nice name)
That looks perfectly what I want to do.
I added all the fields to the fields editor for the table that feeds the dbgrid and added the code to the OnGetText but nothing gets displayed now, not the real field values or the string values??

You said to make the fields persistent fields - is there anything special I have to do to do this (apart from add them to the field editor).

Thanks for your advice, KRis.
 
I have it working....!
Just deleted all the fields in the field editor and re-added them, then deleted all entries in the dbgrid and re-added them and it worked fine.
Think I must have had some settings wrong from previous atempts.

Much apricated advice all, cheers Kris.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top