1

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?

srayner
  • 1,799
  • 4
  • 23
  • 39

2 Answers2

1

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;
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • But what is the ThisRowIsSelected ? – srayner Dec 11 '12 at 16:58
  • Just to clarify, I know how to colour cells, but I don't know how I detect if the row is the currently selected row. – srayner Dec 11 '12 at 17:01
  • Your question specifically asks "how can I simulate to look of row select by highlighting the all the cell of the current row?", which is the question I answered. If you have a different question about how to keep track of selected rows with `dgRowSelect = False`, ask a new question. :-) – Ken White Dec 11 '12 at 17:29
  • 2
    I have an answer to your other question (about detecting if the current row is selected), whenever you get around to posting it as a new question. – Ken White Dec 11 '12 at 17:54
  • Rephrased question here; [link](http://stackoverflow.com/questions/13828991/how-to-keep-track-of-selected-rows-with-dgrowselect-false) Sorry for any confusion. – srayner Dec 11 '12 at 21:48
  • Answered new question, as promised. – Ken White Dec 11 '12 at 21:55
  • @KenWhite http://stackoverflow.com/questions/20170720/dbgrid-custom-draw-gdrowselected-not-working ....... – Jerry Dodge Nov 24 '13 at 04:16
1

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;