Vinod_cmdl
Vinod_cmdl

Reputation: 59

Delphi ClientDataSet Calculated field not updating for specific value

Can anyone please help me to figure out why a calculated field in ClientDataset is not updating with 0.1875. In fact it is not accepting 0.25, 0.50, 0.75, 1.0...etc. Field can be updated if value is 0.26, 0.51...0.1876..etc. I am using following code in Delphi XE3:

procedure TForm1.Button1Click(Sender: TObject);
begin
  ClientDataSet1.Close;
  DBEdit1.DataSource:= DataSource1;
  DBEdit1.DataField := 'PieceRate';
  ClientDataSet1.Open;
end;

procedure TForm1.ClientDataSet1CalcFields(DataSet: TDataSet);
begin
  ClientDataSet1.FieldByName('PieceRate').AsFloat :=StrToFloat(Edit1.Text);
end;

procedure TForm1.ClientDataSet1PieceRateGetText(Sender: TField;
 var Text: string; DisplayText: Boolean);
begin
  if Sender.IsNull then
   Text:='Why it is blank?'
  else
   Text:= Sender.Value;
end; 

enter image description here

enter image description here

enter image description here enter image description here

Upvotes: 0

Views: 1479

Answers (1)

MartynA
MartynA

Reputation: 30715

As you haven't provided an MCVE, I created my own, and its code and DFM extract are below. Its operation should be self-evident - it sets the Value calculated field to the selected value in the ListBox. The point of the ListBox's OnClick handler calling ClientDataSet.First is to cause the dataset to scroll and so invoke its OnCalcFields event.

The project does NOT exhibit the problem you allege (though it's unclear what exactly you mean by the value not being "accepted"). Whichever item in the ListBox is clicked, the corresponding currency value is displayed in the DBEdit preceded by the system's curency symbol, the only minor exception being that 0.1875 is displayed as "£0.19" because by default the value is rounded to two decimal places. My program operates identically in D7 and D10 Seattle, btw.

So, whatever is causing your problem is something in your project that you've not included in your q, which shows if nothing else the value of an MCVE.

In a comment you said:

When I press Calc button, I am just assigning Edit1.text value to calculated field.

Well, that's not what your Button1Click handler does, according to the code in your q. If you are assigning a value to the calculated field anywhere else than in the OnCalcFields event you should be checking that your dataset is in the appropriate State (TDataSetState) before you do so. Are you?

Code:

type
  TForm1 = class(TForm)
    ClientDataSet1: TClientDataSet;
    DataSource1: TDataSource;
    DBEdit1: TDBEdit;
    ClientDataSet1ID: TIntegerField;
    ClientDataSet1Value: TCurrencyField;
    ListBox1: TListBox;
    procedure ClientDataSet1CalcFields(DataSet: TDataSet);
    procedure FormCreate(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
  public
  end;

[...]

procedure TForm1.ClientDataSet1CalcFields(DataSet: TDataSet);
begin
  ClientDataSet1.FieldByName('Value').AsString := ListBox1.Items[ListBox1.ItemIndex];
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ListBox1.ItemIndex := 0;
  ClientDataSet1.CreateDataSet;
  ClientDataSet1.InsertRecord([1]);
end;

procedure TForm1.ListBox1Click(Sender: TObject);
begin
  ClientDataSet1.First;
end;

DFM extract

object DBEdit1: TDBEdit
  DataField = 'Value'
  DataSource = DataSource1
end
object ListBox1: TListBox
  Items.Strings = (
    '0'
    '0.1875'
    '0.25'
    '0.50'
    '0.75'
    '1.0')
  OnClick = ListBox1Click
end
object ClientDataSet1: TClientDataSet
  Aggregates = <>
  Params = <>
  OnCalcFields = ClientDataSet1CalcFields
  object ClientDataSet1ID: TIntegerField
    FieldName = 'ID'
  end
  object ClientDataSet1Value: TCurrencyField
    FieldKind = fkCalculated
    FieldName = 'Value'
    Calculated = True
  end
end
object DataSource1: TDataSource
  DataSet = ClientDataSet1
end

Upvotes: 3

Related Questions