DBGrid custom draw gdRowSelected not working

2019-05-25 08:30发布

问题:

I'm custom drawing in a DBGrid by monitoring OnDrawColumnCell to color the column. When I read the event handler's State, I successfully capture gdSelected and color the font in the selected cell. But when I monitor gdRowSelected, it's never there, and thus I can't tell when a row is selected.

Why doesn't gdRowSelected ever apply? Is this a bug, or intentional functionality?

Here's how I currently draw. When a row is selected, it should show the text in that row in the color red.

procedure TForm1.gItemsDrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  Rec: TRect;
  function C: TCanvas;
  begin
    Result:= gItems.Canvas;
  end;
begin
  //Evaluating gdSelected works fine, but not gdRowSelected
  if gdRowSelected in State then begin
    C.Font.Color:= clRed;
  end else begin
    C.Font.Color:= clWhite;
  end;
  C.Brush.Style:= bsSolid;
  C.Brush.Color:= clBlack;
  C.Pen.Style:= psClear;
  C.FillRect(Rect);
  C.Brush.Style:= bsClear;
  C.Pen.Style:= psSolid;
  Rec:= R;
  DrawText(C.Handle, PChar(Column.Field.AsString), Length(Column.Field.AsString),
    Rec, 0);
end;

The documentation states

gdRowSelected - The row is selected.

but it's never in the State.


EDIT

I tried enabling the option dgRowSelect and it does make a change, but it's still only monitoring the gdSelected enum and never gdRowSelected. With dgRowSelect enabled, gdSelected is in the state for each cell in the entire row. But still gdRowSelected is never in the state.