在DBGrid中选中其中的一条记录使其改颜色,且只改变这一条记录的颜色,其他的记录不变色.请问用代码怎样实现?谢谢各位了!

解决方案 »

  1.   

    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
      with TDbgrid(sender) do
      begin
        if gdSelected in State then
        begin
          dbgrid1.Canvas.Brush.Color:=clyellow; //这行是背景变色
          dbgrid1.Canvas.Font.Color:= clRed;  //这行代码是字变色
        end;
        DefaultDrawColumnCell(rect,datacol,column,state);
      end;
    end;
      

  2.   

    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
      with TDbgrid(sender) do
      begin
        if gdSelected in State then
        begin
          dbgrid1.Canvas.Brush.Color:=clyellow; //这行是背景变色
          dbgrid1.Canvas.Font.Color:= clRed;  //这行代码是字变色
        end;
       
      //DefaultDrawColumnCell(rect,datacol,column,state);
      //应改为
      DefaultDrawDataCell(Rect,Column.Field,state);
      end;
    end;