cxgrid高亮显示(或颜色)已更改窗体closequery上的单元格

时间:2016-06-29 10:32:40

标签: delphi tcxgrid

关于我的表格的接近:

if MessageDlg('Close program ?',
          mtConfirmation, [mbYes,mbCancel],0) <> mrYes then CanClose := False
else if DataModule2.mytable.State in [dsEdit,dsInsert] then
               if MessageDlg('Save changes ?', mtConfirmation,
                    [mbYes,mbNo],0) = mrYes then DataModule2.mytable.Post;

当我触发onclosequery事件时,有没有办法可以在cxgrid中突出显示(或着色)更改的单元格?

我不需要知道更改了什么,只是知道哪个单元格已更改,以便用户可以看到它,以便他可以轻松决定天气以保存更改。

1 个答案:

答案 0 :(得分:1)

让cxGrid绘制以某种方式突出显示的单元格(或行)很简单 cxGrid1DBTableView1CustomDrawCell事件。通过指示OnCloseQuery事件正在进行的标志,您可以将其操作限制在该事件内。

更新我最初使用此答案发布的代码无法成功将当前网格行中的多个单元格标记为已更改。但是,下面更新的代码可以做到这一点;请注意两者中的评论 程序。

type
  TForm1 = class(TForm)
    [...]
  public
    QueryingClose : Boolean;
  end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  try
    QueryingClose := True;
    //{cxGrid1.Invalidate{True);  Do NOT call Invalidate, because it causes the
    //  grid's repainting logic to operate in a way which effectively makes it
    //  impossible to mark more that one cell in the current data row as changed
    ShowMessage('Close?');
  finally
    QueryingClose := False;
  end;
end;

procedure TForm1.cxGrid1DBTableView1CustomDrawCell(Sender:
    TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo:
    TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  Field : TField;
  MarkCell : Boolean;
  S1,
  S2 : String;
  EC : TcxGridTableEditingController;
begin
  if QueryingClose  and
    (TcxGridDBTableView(Sender).DataController.DataSet.State in[dsEdit, dsInsert]) then begin
    Field := TcxGridDBColumn(AViewInfo.Item).DataBinding.Field;
    S1 := VarToStr(Field.OldValue);

    //  When this event is called, the user may be in the middle of editing a cell's contents
    //  So, the purpose of the following lines is to close the inplace editor being used to do
    //  this amd post the chamged value back to the TField associated with the cell
    EC :=  TcxGridDBTableView(Sender).Controller.EditingController;
    if EC.IsEditing then
      EC.HideEdit(True);

    S2 := VarToStr(Field.Value);
    MarkCell := S1 <> S2;
    if MarkCell then
      ACanvas.Brush.Color := clLime;
  end;
end;

为此,您的TDataSet-descendant类型必须支持在其OldValue属性上正确返回字段的原始内容;我用来编写/测试此代码的TClientDataSet肯定会这样做,但我不知道你正在使用的实际TDataSet类型。

希望显而易见的是,您可以使用这两个程序 构建已更改值的TField列表,包括FieldName OldValue和Value。

相关问题