如何为 cxGrid 中的布尔列设置默认值?我的意思是我希望默认情况下所有新行的布尔列的值为“False”
问问题
1184 次
1 回答
1
假设这是一个数据网格(例如包含 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 的布尔列设置初始 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;
于 2020-02-22T17:24:39.647 回答