TcxGrid如何获取修改后的值并更新下一列的值?



我正在使用XE2。我想在用户编辑网格中的单价时,自动重新计算下一列的总价。

我应该使用哪个事件?

我尝试了TCXGridTableView OnEditValueChanged,但我无法获得修改后的值? AItem.EditValue 是修改前的值。

然后我尝试了TcxGridColumn OnEditValueChanged。我可以像这样获取修改后的值:

cxCE := Sender as TcxCurrencyEdit;
cxCE.Value; // this is the modified value

但是,如果用户修改了一个值,然后不按 Enter 键,而是按 TAB 键离开,这里我遇到了一个问题,发生了一个有趣的问题:

  1. TcxGridColumn OnEditValueChanged 事件执行。
  2. 我仍然可以获取 cxCE.Value(修改后的值(并成功更新下一列的值。
  3. 在修改下一列的值后,cxCE.Value 又改回了修改前的值!
  4. 因此,用户输入将回滚,但下一列会更新。

一个例子发生了什么:

数量 |单价 |总价

2......5.............10

当用户将单价从 5 修改为 7 时,按 Tab 键 在 OnEditValueChanged 之后,单价回滚了,但我的逻辑更新了总价:

2、5(回滚( 14(更新(

如果有人可以帮助我,非常感谢。

我应该使用哪个事件?

以上都不是。 我认为你这样做的方式是错误的。 你似乎想要的是 当"数量"或"单价"字段更改时自动更新"总价"字段。 最有成效的考虑方式是数据操作操作, 而不是GUI操作,这就是您应该对其进行编码的方式。

cxGrid 是一个数据库感知组件,这些组件被编码为自动 反映对数据的变化,因此更新总价的方法 字段是在对数据集进行操作的代码中执行此操作,而不是在代码中执行 在 cxGrid 上运行。 如果您尝试在 cxGrid 的代码中执行此操作, 你会发现自己不断地与电网"战斗",因为它知道 如何感知数据库,实际上,你正在试图颠覆它。

尝试下面的示例项目。 设置新的 VCL 项目,添加 TClientDataSet, TDataSource和TDBNavigator,并以通常的方式"连接它们"。

为 CDS 和 FormCreate 设置 OnCalcFields 事件处理程序 事件,然后添加代码 所 示。

当项目运行时,它会动态创建一个 cxGRid 来显示数据(我做到了 这样,是因为 cxGrid 中有太多的设置和子组件, 最简单的方法是在代码中创建一个,而不是在这样的答案中指定其设置(。

尝试更改"数量"和"单价"字段中的值,以及 请注意,总价会自动更新,无需任何代码 在 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;

最新更新