在OnDrawDataCell事件中写代码即可,这个问题也讨论过很多次了,请搜索一下“stringgrid“或者“Dbgrid“!

解决方案 »

  1.   

    procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
          DataCol: Integer; Column: TColumn; State: TGridDrawState);
    var
      ARect: TRect;
      PointArray : array [0..2] of TPoint;
      P: TPoint;
    begin
      inherited;
      ARect := Rect;
      with DBGrid1 do
      begin
        if (gdSelected in State) then
        begin
          Canvas.Brush.Color := $00D1F2FC;
          Canvas.Font.Color := clBlue;
        end;
        Canvas.FillRect(Rect);
        if DataCol = 0 then
        begin
          {aRect := Rect;
          InflateRect(aRect, 1, 1);
          Canvas.Brush.Color := $00D1F2FC;
          Canvas.FillRect(Rect);}
          DrawText(Canvas.Handle, PChar('    ' + Column.Field.Text), Length('    ' + Column.Field.Text), ARect, DT_SINGLELINE or DT_VCENTER or DT_NOPREFIX)
        end
        else
          DrawText(Canvas.Handle, PChar(Column.Field.Text), Length(Column.Field.Text), ARect, DT_SINGLELINE or DT_VCENTER or DT_NOPREFIX);
        if (gdSelected in State) and (DataCol = 0) then
        begin
          P.x := Rect.Left + 3;
          P.y := Rect.Top + (Rect.Bottom - Rect.Top) div 2;
          PointArray[0] := Point(P.x, P.y + 3);
          PointArray[1] := Point(P.x, P.y - 3);
          PointArray[2] := Point(P.x + 3, P.y);
          Canvas.Brush.Color := clBlue;
          Canvas.Pen.Color := clBlue;
          Canvas.Polygon(PointArray);
        end;
      end;end;
    面目全非,你件有用的摘。多试试!
      

  2.   

    看我的:
    //...
      private
        Lx: String;
    在DBGrid1的DrawColumnCell事件中加入:
    procedure TTemplyForm.DBGrid1DrawColumnCell(Sender: TObject;
      const Rect: TRect; DataCol: Integer; Column: TColumn;
      State: TGridDrawState);
    begin
    if Table1.FieldByName('LOGINID').AsString=Lx then
    begin
      DBGrid1.Canvas.Brush.Color:=clRed;
      DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
    end;
    end;在DataSource1的DataChange事件中加入:
    procedure TTemplyForm.DataSource1DataChange(Sender: TObject;
      Field: TField);
    begin
    Lx:=Table1.FieldByName('LOGINID').AsString;
    end;希望你不要把上面太多的代码拷贝进去!LOGINID是主索引!
    其它的再参考前面的了!
      

  3.   

    不如设置DBGrid的Option属性:设dbRowSelect为true
      

  4.   

    简单办法:
    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
     if (gdSelected in State) then
          begin
            DBGrid1.Canvas.Brush.Color := clTeal;//选中
            DBGrid1.Canvas.Font.Color :=clWhite ;//字体
          end
    end;
      

  5.   

    忘了一点,要先将dbgrid的option设dbrowselect为true