Иногда возникают случаи, когда необходимо выбрать некоторые записи с помощью CheckBox`ов и работать только с отмеченными записями. “Выбрано/Не выбрано” в базе хранить неразумно, тем более, если приложение многопользовательское. Для работы с информацией об отмеченных записях правильнее создать unbound столбец с Properties=CheckBox и отдельно список для хранения состояний (статусов) комбобоксов в этом столбце.
В классе формы определяем:
private
AList: TList;
function CheckList(ARecord: TcxCustomGridRecord): Boolean;
function TForm1.CheckList(ARecord: TcxCustomGridRecord): Boolean;
begin
Result := AList.IndexOf(Pointer(ARecord.RecordIndex)) <> - 1;
end;
При создании формы - создаем список, при закрытии - освобождаем память:
procedure TForm1.FormCreate(Sender: TObject);
begin
AList := TList.Create;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
AList.Free;
end;
При клике мышкой на ячейке с чекбоксом выполняется следующее:
procedure TForm1.cxGrid1DBTableView1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
AHitTest: TcxCustomGridHitTest;
AGridRecord: TcxCustomGridRecord;
begin
if Sender is TcxGridSite then
begin
with TcxGridSite(Sender).GridView do
AHitTest := ViewInfo.GetHitTest(X, Y);
if (AHitTest.HitTestCode = htCell) and (TcxGridDBColumn(TcxGridRecordCellHitTest(AHitTest).Item).DataBinding.FieldName = '') then
AGridRecord := TcxGridRecordCellHitTest(AHitTest).GridRecord
else
Exit;
end;
if (AGridRecord <> nil) then
if CheckList(AGridRecord) then
AList.Remove(Pointer(AGridRecord.RecordIndex))
else
AList.Add(Pointer(AGridRecord.RecordIndex));
end;
Отрисовка чекбокса в зависимости от его статуса, хранящегося в AList:
procedure TForm1.cxGrid1DBTableView1DBColumn1CustomDrawCell(
Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
if AViewInfo.EditViewInfo is TcxCustomCheckBoxViewInfo then
TcxCustomCheckBoxViewInfo(AViewInfo.EditViewInfo).State := TcxCheckBoxState(CheckList(AViewInfo.GridRecord));
end;
Отмеченные записи можно выделить цветом:
procedure TForm1.cxGrid1DBTableView1CustomDrawCell(
Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
with AViewInfo do
if CheckList(GridRecord) then
ACanvas.Brush.Color := clHighlight
else
ACanvas.Brush.Color := clWindow;
ACanvas.Font.Color := clBlack;
end;