各位看明白了吗?
我的代码如下:
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  if (DBGrid1.DataSource.DataSet.RecNo mod 2)<>0 then  begin
     DBGrid1.Canvas.Brush.Color:=clwhite;
  end
  else begin
     DBGrid1.Canvas.Brush.Color:=clCream;
  end;
  DBGrid1.DefaultDrawColumnCell(REct,DataCol,Column,State);
end;结果呢,我选种哪行 ,哪行的字就都看不见了,实际是字的颜色根据反显效果变成了白色,但是背景色却不改变。
各位请帮忙!

解决方案 »

  1.   

    需要根据 State: TGridDrawState 区别对待
      

  2.   

    要根据TGridDrawState的值来判断当前要画的cell的属性,
    gdSelected:cell被选中
    gdFocused :cell获得焦点
    gdFixed:固定cell,一般为标题栏
    像你这个情况应当在gdSelected中重新设置被选中后的背景色和字体
    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
      if (DBGrid1.DataSource.DataSet.RecNo mod 2)<>0 then  begin
         DBGrid1.Canvas.Brush.Color:=clwhite;
      end
      else begin
         DBGrid1.Canvas.Brush.Color:=clCream;
      end;
      if (State=gdSelected) then begin
         DBGrid1.Canvas.Brush.Color:=clBlue;
         DBGrid1.Canvas.Font.Color:= clWhite;
      end;
      DBGrid1.DefaultDrawColumnCell(REct,DataCol,Column,State);
    end;
      

  3.   

    To duduwolf(嘟嘟狼) :
      还是不行,我想让整行的颜色都这样,但是现在我的RowSelected 属性是真的,现在的效果是第一列的字体还是白色,还是看不见。