怎么设置DBGrid中某一行的颜色啊?
其中这一行的选择是通过行的某一字段值来确定的

解决方案 »

  1.   

    转贴
    使dbgrid的某几笔资料变色你可在 DBGrid 元件的 DrawDataCell 事件中依资料的条件性来改变格子或文字的颜色.
    如 :OnDrawDataCell(...)
    begin
    with TDBGrid(Sender) do
    begin
      if (条件) then
       Canvas.TextOut(Rect.Left + 4
      Rect.Top + 2'要显示的文字如表格的资料');
    end;  而你会看到 DBGrid 的显示资料怎麽有重叠的情况那是因为原本DBGrid要显示的资料与 TextOut 所显示的资料重叠
      解决方法 :
      在 Query 元件所加入的栏位(在元件上按右键会有 Add Fields...的选单)在不要显示资料的栏位的 OnGetText 事件中有一参数设定为 False;procedure TForm1.Query1Detail1GetText(Sender: TField; var Text: string;
    DisplayText: Boolean);
    begin
    // 决定在 DBGrid 得知表格资料时要不要显示所得到的资料False -> 不显示
    // 就可避免与 TextOut 的文字重叠了
    DisplayText : = False;
    end;
    end;  如果用 Delphi 3 处理很简单.例如:对表中某字段当其数值小于0时为红字其他为黑字.
    在 DBGrid.OnDrawColumnCell(...) 中:begin
    if TableField.AsInteger < 0 then
      DBGrid.Canvas.Font.Color := clRed
    else
      DBGrid.Canvas.Font.Color := clBlack;
    DBGrid.DefaultDrawColumnCell(...);
    end;这样对 Field 指定的格式仍旧生效不必重写. 
      

  2.   

    // DBGrid1的OnDrawColumnCell事件
    if ((State = [gdSelected]) or (State=[gdSelected,gdFocused])) then
        begin
          DBGrid1.Canvas.Font.Color :=ClYellow;
          DBGrid1.Canvas.Brush.Color :=clblue;  
          DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
     end;
    DBGrid1.Canvas.Font.Color :=ClYellow;//字体颜色,可以改成你想要的
    DBGrid1.Canvas.Brush.Color :=clblue;  //背景颜色,可以改成你想要的