简化后,我有一个 cxGrid,我在其中键入开始时间和结束时间。我需要的是一个函数,当我更改其中一个值时计算差异并将其存储在第三列中
我很难找到正确的方法。
简化后,我有一个 cxGrid,我在其中键入开始时间和结束时间。我需要的是一个函数,当我更改其中一个值时计算差异并将其存储在第三列中
我很难找到正确的方法。
I guess you have assigned some sort of Properties editor (probably a DateEdit) to the column.
Given that, you could try to use following code in the OnValidate event of the Properties editor:
var
ValueThirdCol : variant;
RecordIndex : integer;
begin
RecordIndex := myView.DataController.FocusedRecordIndex;
ValueThirdCol := myView.DataController.GetValue(RecordIndex, MyEndDateCol.Index) - myView.DataController.GetValue(RecordIndex, MyStartDateCol.Index);
myView.DataController.SetValue(RecordIndex, myDifCol.Index, ValueThirdCol);
end;
Please note that you might have to tweak this code a bit depending on if you have set GridMode or DataModeController.SyncMode to true, or not, and to use DisplayValue where necessary, but the basic idea should work.
EDIT: the OnValidate event of the Properties editor occurs before converting the display value to the edit value. That is the reason why this code I provided had to be tweaked.
In order for the code to work, you need to use (for the column being modified) the DisplayValue argument provided by the event instead of the value returned by GetValue.
For example, if the EndDateCol would be the column that triggered the OnValidate, then the code should be
ValueThirdCol := DisplayValue - myView.DataController.GetValue(RecordIndex, MyStartDateCol.Index);
HTH