delphi - 如何为 cxGrid 中的列设置默认值?
问题描述
如何为 cxGrid 中的布尔列设置默认值?我的意思是我希望默认情况下所有新行的布尔列的值为“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 的布尔列设置初始 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;
推荐阅读
- android - Android:ScrollView包含两个嵌套的ConstraintLayouts,无法水平填充屏幕
- scala - 从时间戳范围内获取开始日期和结束日期
- javascript - 数组 - 为什么即使它们(对我而言)看起来相同,也可以使用一个项目而不是另一个?
- python - Python-redis:使用 decode_responses=True 设置客户端后获取二进制数据
- sql - 将两列的值以逗号分隔
- automation - 在量角器中创建了一个跑步者,但在通过第一次测试后出错了?
- java - 为一类设置命名策略为小写驼峰式
- javascript - 渲染时如何使用onClick
- python - 一个 QObject 被移动到它自己的线程,但是槽没有在这个线程中执行 - 我做错了什么,或者我的理解有误?
- android - 如何在 Android apk 文件中签署 CERT.RSA/CERT.SF?