I see (from the comment chain) that you're wanting to call either the generic or (in certain cases) also call the code for the checkbox drawing.
the generic OnDrawColumnCell handler handles the background colour for the cell. How can I extend this to create a handler which will also draw checkboxes when they're needed? Incidentally, the generic code has to be called before the checkbox code, not after.
That's actually quite simple. Define both methods with the proper signature (the generic one, and the checkbox one) (code untested!). Only connect the generic event to the TDBGrid.OnDrawColumnCell
events, though - the checkbox one will be chained in if needed:
// Generic (from my other post) - notice method name has changed
procedure TDataModule1.GenericDrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
const
RowColors: array[Boolean] of TColor = (clSilver, clDkGray);
var
OddRow: Boolean;
Grid: TDBGrid;
begin
if (Sender is TDBGrid) then
begin
Grid := TDBGrid(Sender);
OddRow := Odd(Grid.DataSource.DataSet.RecNo);
Grid.Canvas.Brush.Color := RowColors[OddRow];
Grid.DefaultDrawColumnCell(Rect, DataCol, Column, State);
// If you want the check box code to only run for a single grid,
// you can add that check here using something like
//
// if (Column.Index = 3) and (Sender = DBGrid1) then
//
if (Column.Index = 3) then //
CheckBoxDrawColumCell(Sender, Rect, DataCol, Column, State)
end;
end;
// Checkbox (from yours) - again, notice method name change.
procedure TEditDocket.CheckBoxDrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
DrawRect: TRect;
Grid: TDBGrid;
begin
// Don't use DBGrid1, because it makes the code specific to a single grid.
// If you need it for that, make sure this code only gets called for that
// grid instead in the generic handler; you can then use it for another
// grid later (or a different one) without breaking any code
if column.Index = 3 then
begin
Grid := TDBGrid(Sender);
DrawRect:= Rect;
Drawrect.Left := Rect.Left + 24;
InflateRect (DrawRect, -1, -1);
Grid.Canvas.FillRect (Rect);
DrawFrameControl (Grid.Canvas.Handle, DrawRect, DFC_BUTTON,
ISChecked[Column.Field.AsInteger]); // Don't know what ISChecked is
end;
// The below should no longer be needed, because DefaultDrawColumnCell has
// been called by the generic handler already.
//
// else
// Grid.DefaultDrawColumnCell (Rect, DataCol, Column, State);
end;
After seeing this comment you made to Sertac:
in one grid, it could be column 3 whereas it could be column 4 which needs to be drawn as a checkbox.
I've offered one way to address that in my code above (see the comment in GenericDrawColumnCell
). An alternative (provided you only have one column in each grid that needs a checkbox) is to indicate the column that contains the checkbox in the TDBGrid.Tag
property:
if (Column.Index = Grid.Tag) then