RowSelect breaks the functionality of OnCellClick, so I need to turn RowSelect off. So then how can I simulate to look of row select by highlighting the all the cell of the current row?
This worked for me ( dgRowSelect=False
and dgMultiSelect=False
):
Declare hack types for DBGrid
and GridDataLink
to access protected methods
and two variables
type
THackGrid = class(TDBGrid);
THackDataLink = class(TGridDataLink);
var
HackGrid: THackGrid;
HackDataLink: THackDataLink;
In OnFormCreate
assign the variables to have them available at drawing time:
procedure TMyForm.FormCreate(Sender: TObject);
begin
HackGrid := THackGrid(MainGrid);
HackDataLink := THackDataLink(HackGrid.DataLink);
end;
and test selected row using TGridDataLink.GetActiveRecord
:
procedure TMyForm.MainGridDrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
begin
if Not (gdSelected in State) and (HackGrid.Row = HackDataLink.GetActiveRecord + 1) then
MainGrid.Canvas.Brush.Color := clInfoBk;
MainGrid.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
Use the TDBGrid.OnDrawColumnCell
event, and set the State
to indicate the row is selected.
procedure TfrmPrimaryCare.dbGrdPCClaimsDrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
NewState: TGridDrawState;
RowSelected: Boolean;
begin
NewState := State;
RowSelected := (Sender as TDBGrid).SelectedRows.CurrentRowSelected;
if (RowSelected) then
NewState := NewState + [gdSelected];
TDBGrid(Sender).DefaultDrawColumnCell(Rect, DataCol, Column, NewState);
end;
来源:https://stackoverflow.com/questions/13824573/how-can-i-colour-whole-row-in-dbgrid-with-rowselect-turned-off