cxGrid 实现 checkbox(无数据库记录)
SQL语句
Query.SQL.add('select *,'N' as Selected from make order by name ')
进入QUERY字段编辑器
将 Selected 的 FieldKind 设置为 fkInternalCalc
确保 readonly 为 false
给 cxGrid1DBTableView1 新建一列,选择 fielname 设为 Selected
ValueCheck := 'Y' , ValueunCheck := 'N'
鼠标点击事件1
procedure TEditProduct.cxGrid1DBTableView1MouseUp(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
Row : Integer;
begin
Row := cxGrid1DBTableView1.DataController.FocusedRecordIndex;
//cxGrid1DBTableView1.DataController.DataSet.Edit;
//cxGrid1DBTableView1.DataController.DataSet.FieldByName('Selected').AsString := 'Y';
if cxGrid1DBTableView1.ViewData.Records[Row].Values[1] = 'Y' then
cxGrid1DBTableView1.ViewData.Records[Row].Values[1] := 'N'
else
cxGrid1DBTableView1.ViewData.Records[Row].Values[1] := 'Y';
end;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
Row : Integer;
begin
Row := cxGrid1DBTableView1.DataController.FocusedRecordIndex;
//cxGrid1DBTableView1.DataController.DataSet.Edit;
//cxGrid1DBTableView1.DataController.DataSet.FieldByName('Selected').AsString := 'Y';
if cxGrid1DBTableView1.ViewData.Records[Row].Values[1] = 'Y' then
cxGrid1DBTableView1.ViewData.Records[Row].Values[1] := 'N'
else
cxGrid1DBTableView1.ViewData.Records[Row].Values[1] := 'Y';
end;
鼠标点击事件2
procedure TorderMore1.cxGrid1DBTableView1CellClick(
Sender: TcxCustomGridTableView;
ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
AShift: TShiftState; var AHandled: Boolean);
var
Row : Integer;
begin
if ACellViewInfo.Item.Name ='ColSelect' then
begin
Row := cxGrid1DBTableView1.DataController.FocusedRecordIndex;
if cxGrid1DBTableView1.ViewData.Records[Row].Values[15] = 'Y' then
cxGrid1DBTableView1.ViewData.Records[Row].Values[15] := 'N'
else
cxGrid1DBTableView1.ViewData.Records[Row].Values[15] := 'Y';
end;
end;
Sender: TcxCustomGridTableView;
ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
AShift: TShiftState; var AHandled: Boolean);
var
Row : Integer;
begin
if ACellViewInfo.Item.Name ='ColSelect' then
begin
Row := cxGrid1DBTableView1.DataController.FocusedRecordIndex;
if cxGrid1DBTableView1.ViewData.Records[Row].Values[15] = 'Y' then
cxGrid1DBTableView1.ViewData.Records[Row].Values[15] := 'N'
else
cxGrid1DBTableView1.ViewData.Records[Row].Values[15] := 'Y';
end;
end;
初始化
procedure TEditProduct.FormActivate(Sender: TObject);
var
I : integer;
MakeMK : string;
thisID : string;
begin
if ID > 0 Then
MakeMK := ',' + '1,2,3,4,5'+ ',';
ShowMessage(MakeMK);
begin
for I :=0 to cxGrid1DBTableView1.ViewData.RecordCount-1 do
begin
thisID := ','+varToStr(cxGrid1DBTableView1.ViewData.Records[I].Values[0])+',';
//ShowMessage(thisID);
if AnsiStrPos(PChar(MakeMK), PChar(thisID)) <> nil then
cxGrid1DBTableView1.ViewData.Records[I].Values[1] := 'Y';
end;
end;
end;
procedure TEditProduct.cxGrid1DBTableView1StylesGetContentStyle(
Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
begin
if ARecord.Values[1] = 'Y' then
AStyle := cxStyle2;
end;
var
I : integer;
MakeMK : string;
thisID : string;
begin
if ID > 0 Then
MakeMK := ',' + '1,2,3,4,5'+ ',';
ShowMessage(MakeMK);
begin
for I :=0 to cxGrid1DBTableView1.ViewData.RecordCount-1 do
begin
thisID := ','+varToStr(cxGrid1DBTableView1.ViewData.Records[I].Values[0])+',';
//ShowMessage(thisID);
if AnsiStrPos(PChar(MakeMK), PChar(thisID)) <> nil then
cxGrid1DBTableView1.ViewData.Records[I].Values[1] := 'Y';
end;
end;
end;
procedure TEditProduct.cxGrid1DBTableView1StylesGetContentStyle(
Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
begin
if ARecord.Values[1] = 'Y' then
AStyle := cxStyle2;
end;
提交结果
for I :=0 to cxGrid1DBTableView1.ViewData.RecordCount-1 do
begin
T := varToStr(cxGrid1DBTableView1.ViewData.Records[I].Values[1]);
if T = 'Y' then
NewMake := NewMake + ',' + varToStr(cxGrid1DBTableView1.ViewData.Records[I].Values[0]);
begin
T := varToStr(cxGrid1DBTableView1.ViewData.Records[I].Values[1]);
if T = 'Y' then
NewMake := NewMake + ',' + varToStr(cxGrid1DBTableView1.ViewData.Records[I].Values[0]);