在DBGRID的ONDRAWDATACELL中写如下代码: if gdselected in state then begin dbgrid1.Canvas.Brush.Color:=clred; //选中的单元格的颜色 dbgrid1.Canvas.FillRect(rect); dbgrid1.Canvas.Pen.Color:=clwhite; //选中的单元格的字体颜色 dbgrid1.Canvas.TextOut(rect.left,rect.top,field.value); end;在D5下测试通过。
在DBGrid的OnDrawDataCell事件里可以改变单元格的颜色 某一列的颜色可直接改procedure TForm1.Button1Click(Sender: TObject); begin query1.close; query1.sql.text:='select * from animals'; query1.open; end;procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect; Field: TField; State: TGridDrawState); begin DBGrid1.Canvas.Font.Color:=clred; DBGrid1.Canvas.Brush.Color:=clgray; {//改变某个单元格的颜色 if (Field.FullName='SIZE')and (Field.AsInteger=2) then begin DBGrid1.Canvas.TextRect(rect,rect.Left,rect.Top,Field.AsString); end;} //改变某行的颜色,该行的NAME字段值=aaa if Field.DataSet.FieldByName('NAME').asString='aaa' then DBGrid1.Canvas.TextRect(Rect,rect.Left,rect.Top,Field.AsString); end;procedure TForm1.Button2Click(Sender: TObject); begin //改变某个列的颜色 DBGrid1.Columns[1].Color:=clgreen; DBGrid1.Columns[1].Font.color:=clred; end;
行号:=DBGrid1.SelectedField.DataSet.RecNo;
列:=DBGrid1.SelectedField.FieldName;在OnDrawDataCell判断是否是你记录的行、列,
DBGrid1.Canvas.Font.Color := clRed;
DBGrid1.Canvas.Brush.Color := clyellow;
(Sender as TDBGrid).Canvas.TextRect(rect,rect.Left+2,rect.Top+2,field.AsString);
if gdselected in state then
begin
dbgrid1.Canvas.Brush.Color:=clred; //选中的单元格的颜色
dbgrid1.Canvas.FillRect(rect);
dbgrid1.Canvas.Pen.Color:=clwhite; //选中的单元格的字体颜色
dbgrid1.Canvas.TextOut(rect.left,rect.top,field.value);
end;在D5下测试通过。
某一列的颜色可直接改procedure TForm1.Button1Click(Sender: TObject);
begin
query1.close;
query1.sql.text:='select * from animals';
query1.open;
end;procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
begin
DBGrid1.Canvas.Font.Color:=clred;
DBGrid1.Canvas.Brush.Color:=clgray; {//改变某个单元格的颜色
if (Field.FullName='SIZE')and (Field.AsInteger=2) then
begin
DBGrid1.Canvas.TextRect(rect,rect.Left,rect.Top,Field.AsString);
end;} //改变某行的颜色,该行的NAME字段值=aaa
if Field.DataSet.FieldByName('NAME').asString='aaa' then
DBGrid1.Canvas.TextRect(Rect,rect.Left,rect.Top,Field.AsString);
end;procedure TForm1.Button2Click(Sender: TObject);
begin
//改变某个列的颜色
DBGrid1.Columns[1].Color:=clgreen;
DBGrid1.Columns[1].Font.color:=clred;
end;