2

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.

Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327
  • Related, but not duplicate: http://stackoverflow.com/questions/13824573/how-can-i-colour-whole-row-in-dbgrid-with-rowselect-turned-off?rq=1 – Jerry Dodge Nov 24 '13 at 04:09
  • Does [this](http://stackoverflow.com/a/13829114/62576) help? Funny: I posted this link as your comment hit my inbox. :-) – Ken White Nov 24 '13 at 04:17
  • Yes, it helps, and I saw it prior to asking my question, but it still doesn't explain why `gdRowSelected` doesn't seem to do anything – Jerry Dodge Nov 24 '13 at 04:19
  • It does, actually. You have to check `TDBGrid.SelectedRows.CurrentRowSelected` instead, as that answer shows. `gdSelected` indicates that the *cell* should be considered selected, and `CurrentRowSelected` indicates that the entire *row* is selected (when `dgRowSelect` is included in `Options`). – Ken White Nov 24 '13 at 04:22
  • @KenWhite Understood, and that does work, but why would `gdRowSelected` be in the `State` for no reason? Isn't that what it's meant for? – Jerry Dodge Nov 24 '13 at 04:24
  • I have no idea why it doesn't work. :-) I just know that the solution in that post does, and so it's an acceptable workaround to me. – Ken White Nov 24 '13 at 04:55
  • @Jerry - Regarding your edited edit, not that it invalidates the question, but then you don't really need 'gdRowSelected', do you? – Sertac Akyuz Nov 24 '13 at 20:06
  • @SertacAkyuz I'm trying to figure out why it never does anything – Jerry Dodge Nov 24 '13 at 20:08

0 Answers0