我应该使用哪个事件?
以上都不是。我认为你正在以错误的方式解决这个问题。你似乎想要的是
当 Qty 或 UnitPrice 字段更改时自动更新 TotalPrice 字段。
最有效的思考方式是数据操作,
而不是一个 GUI 操作,这就是你应该编码的方式。
cxGrid 是一个 db-aware 组件,这些组件被编码为自动
反映对数据的更改,因此更新 TotalPrice 的方法
字段是在对数据集进行操作的代码中执行,NOT 在代码中
它在 cxGrid 上运行。如果您尝试在 cxGrid 的代码中执行此操作,
你会发现自己不断地与网格“战斗”,因为它知道
如何成为 db-aware 而你实际上是在尝试颠覆它。
试试下面的示例项目。新建一个VCL工程,添加一个TClientDataSet,
TDataSource 和 TDBNavigator 并以通常的方式“连接”它们。
为 CDS 和 FormCreate 设置 OnCalcFields 事件处理程序
表单的事件,然后添加代码
如下所示。
项目运行时动态创建一个cxGRid来显示数据(我做了
这种方式是因为在 cxGrid 中有太多的设置和子组件
最简单的方法是在代码中创建一个,而不是在这样的答案中指定其设置。
尝试更改 Qty 和 UnitPrice 字段中的值,然后
请注意,TotalPrice 会自动更新而无需任何代码
在 cxGrid 上运行。
type
TForm1 = class(TForm)
CDS1: TClientDataSet;
DS1: TDataSource;
DBNavigator1: TDBNavigator;
procedure FormCreate(Sender: TObject);
procedure CDS1CalcFields(DataSet: TDataSet);
private
public
cxGrid : TcxGrid;
cxLevel : TcxGridLevel;
cxView : TcxGridDBTableView;
end;
[...]
// This is a utility function to create TFields in code
function CreateField(AFieldClass : TFieldClass; AOwner : TComponent; ADataSet : TDataSet;
AFieldName, AName : String; ASize : Integer; AFieldKind : TFieldKind) : TField;
begin
Result := AFieldClass.Create(AOwner);
Result.FieldKind := AFieldKind;
Result.FieldName := AFieldName;
Result.Name := AName;
Result.Size := ASize;
Result.DataSet := ADataSet;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i : Integer;
Field : TField;
Col : TcxGridDBColumn;
begin
// First, create the Fields of the ClientDataSet
Field := CreateField(TIntegerField, Self, CDS1, 'ID', 'CDS1ID', 0, fkData);
Field := CreateField(TIntegerField, Self, CDS1, 'Qty', 'CDS1Qty', 0, fkData);
Field := CreateField(TCurrencyField, Self, CDS1, 'UnitPrice', 'CDS1UnitPrice', 0, fkData);
Field := CreateField(TCurrencyField, Self, CDS1, 'TotalPrice', 'CDS1TotalPrice', 0, fkInternalCalc);
// Field.ReadOnly := True;
CDS1.CreateDataSet;
CDS1.IndexFieldNames := 'ID';
// Next, populate the CDS with a few records
// Note : If we are using calculated fields, we do to need to specify
// a value for the TotalPriced field
CDS1.InsertRecord([1, 1, 1]);
CDS1.InsertRecord([2, 2, 5]);
CDS1.InsertRecord([3, 3, 6]);
CDS1.First;
// Now, create a cxGrid to display the CDS data
cxGrid := TcxGrid.Create(Self);
cxGrid.Parent := Self;
cxGrid.Width := 400;
cxLevel := cxGrid.Levels.Add;
cxLevel.Name := 'Firstlevel';
cxView := cxGrid.CreateView(TcxGridDBTableView) as TcxGridDBTableView;
cxView.Name := 'ATableView';
cxView.DataController.KeyFieldNames := 'ID';
cxView.DataController.Options := cxView.DataController.Options + [dcoImmediatePost];
cxLevel.GridView := cxView;
cxView.DataController.DataSource := DS1;
cxView.DataController.CreateAllItems;
// Since the TotalPrice column is a calculated field, we need to
// prevent the user from attempting to edit it
Col := cxView.GetColumnByFieldName('TotalPrice');
Col.Options.Editing := False;
ActiveControl := cxGrid;
end;
// Procedure to calculate the TotalPrice field
procedure CalculateTotalPrice(DataSet : TDataSet);
var
Qty : Integer;
UnitPrice,
TotalPrice : Currency;
begin
Qty := DataSet.FieldByName('Qty').AsInteger;
UnitPrice := DataSet.FieldByName('UnitPrice').AsCurrency;
TotalPrice := Qty * UnitPrice;
DataSet.FieldByName('TotalPrice').AsCurrency := TotalPrice;
end;
procedure TForm1.CDS1CalcFields(DataSet: TDataSet);
begin
CalculateTotalPrice(DataSet);
end;