Format a field

Format a field

Postby john_kuiper » Wed 14 Nov 2012 09:12

I have created fields in TVirtualtable in code.
There is a TFloatfield created and using TMyQuery gives a fieldproperty Editmask to change the displayview. mask ###.## for amount 100 gives 100.00.

TVirtualTable.fields[0].Editmask is compiling, but gives a runtime error.

How can I format the text of my field?

I'm using MyDAC 7.5.10
john_kuiper
 
Posts: 19
Joined: Tue 15 May 2012 09:34

Re: Format a field

Postby AlexP » Wed 14 Nov 2012 11:30

Hello,

This problem is due to the fact that the fields are not created before the explicit opening of VirtualTable (VirtualTable.Open), when attempting to address to a non-existent field, an error occurs. To solve the problem, you should create these fields:

Code: Select all
  MyQuery1.Open;
  VirtualTable1.Assign(MyQuery1);

  for i := 0  to VirtualTable1.FieldDefs.Count -1 do
    VirtualTable1.FieldDefs.Items[i].CreateField(VirtualTable1);
  VirtualTable1.Fields[0].EditMask  := '###.##';
  VirtualTable1.Open;
AlexP
Devart Team
 
Posts: 5528
Joined: Tue 10 Aug 2010 11:35

Re: Format a field

Postby john_kuiper » Wed 14 Nov 2012 12:41

Thanks. It wil not create a runtime error.
But the format isn't changed.

(Virtualtable1.Fields[4] as TFloatField).DisplayFormat := '##0.00' is the trick
john_kuiper
 
Posts: 19
Joined: Tue 15 May 2012 09:34

Re: Format a field

Postby AlexP » Wed 14 Nov 2012 14:18

Hello,

The EditMask property is responsible not for display format, but for input format. To modify display format, the DisplayFormat property should be used ether bringing the field to a correspondent type (as in your example), or creating a variable of this type, for example:

Code: Select all
  VirtualTable1Sal := TFloatField.Create(VirtualTable1);
  VirtualTable1Sal.Name := 'sal';
  VirtualTable1Sal.FieldName := 'sal';
  VirtualTable1Sal.DisplayFormat := '####.##';
  VirtualTable1Sal.DataSet := VirtualTable1;
  VirtualTable1.Open;
AlexP
Devart Team
 
Posts: 5528
Joined: Tue 10 Aug 2010 11:35


Return to VirtualDAC