繁体   English   中英

如何为 cxGrid 中的列设置默认值?

[英]how to set a default value for a column in cxGrid?

如何为 cxGrid 中的 boolean 列设置默认值? 我的意思是我希望默认情况下所有新行都为我的 boolean 列设置“False”值

假设这是一个数据网格(包含例如 cxGridDBTableView),您应该在数据集的 OnNewRecord 事件中设置任何默认值,如下所示

procedure TForm1.ClientDataSet1NewRecord(DataSet: TDataSet);
var
  Field : TField;
  i : Integer;
begin
  // set all Boolean data fields to False
  for i := 0 to DataSet.FieldCount - 1 do begin
    Field := DataSet.Fields[i];
    if (Field.DataType = ftBoolean) and (Field.FieldKind = fkData) then
    // the test on Field.FieldKind is to avoid disturbing any fkCalculated or fkInternalCalc fields
      Field.AsBoolean := False;
  end;
end;

如果您这样做(或在 OnNewRecord 事件中设置任何其他字段值),这些值将自动传输到 cxGrid。

更新下面显示了如何为未绑定的 cxGridTableView 的 boolean 列设置初始 False 值。 注意:代码创建了 TableView,因此无需将它或 cxGrid 添加到表单。

    // form fields (of Form1)
    cxGrid1 : TcxGrid;
    cxLevel : TcxGridLevel;
    TableView : TcxGridTableView;
    Col1,
    Col2,
    Col3 : TcxGridColumn;
  end;

 [...]
procedure TForm1.FormCreate(Sender: TObject);
begin
  cxGrid1 := TcxGrid.Create(Self);
  cxGrid1.Parent := Self;

  cxLevel := cxGrid1.Levels.Add;
  cxLevel.Name := 'Firstlevel';

  TableView := TcxGridTableView.Create(Self);
  TableView := cxGrid1.CreateView(TcxGridTableView) as TcxGridTableView;
  TableView.Name := 'ATableView';
  TableView.Navigator.Visible := True;

  Col1 := TableView.CreateColumn;
  Col1.DataBinding.ValueType := 'Integer';
  Col1.Caption := 'RowID';

  Col2 := TableView.CreateColumn;
  Col2.DataBinding.ValueType := 'String';
  Col2.Caption := 'RowName';

  Col3 := TableView.CreateColumn;
  Col3.DataBinding.ValueType := 'Boolean';
  Col3.Caption := 'RowChecked';

  cxLevel.GridView := TableView;

  TableView.DataController.OnNewRecord := cxGridTableViewDataControllerNewRecord;
end;

procedure TForm1.cxGridTableViewDataControllerNewRecord(
  ADataController: TcxCustomDataController; ARecordIndex: Integer);
begin
 Caption := IntToStr(ARecordIndex);
  ADataController.Values[ARecordIndex, 2] := False;  // The 2 is the index of Col3
end;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM