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
with DBGrid1 do
begin
Canvas.Font.Color:=clRed;
Canvas.Brush.Color:=clYellow;
end;
end;
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;
function get_dbg_edit_text(dbgrid :tdbgrid):TEdit;
var i:integer;
begin
result := nil;
if not assigned(dbgrid) then exit;
if not dbgrid.EditorMode then
begin
dbgrid.EditorMode:=true;
dbgrid.EditorMode:=false;
end;
with dbgrid do for i:=0 to pred(componentcount) do
if components[i] is Tcustomedit then
begin
result := tedit(components[i]);
break;
end;
end;
function get_dbg_edit(dbgrid : tdbgrid): string;
var
dbg_edit :TEdit;
begin
dbg_edit:=get_dbg_edit_text(dbgrid);
if assigned(dbg_edit) then
result := dbg_edit.text
else result:='';
end;
procedure TForm1.DBGrid1Enter(Sender: TObject);
{显示当前编辑框背景颜色}
var
dbg_edit : tedit;
begin
dbg_edit:=get_dbg_edit_text(tdbgrid(sender));
if assigned(dbg_edit) then dbg_edit.color:=clyellow;
end;
procedure TQueryRepForm.DBGrid1DrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
var
i :integer;
begin
if gdSelected in State then
Exit;
//定义表头的字体和背景颜色:
for i :=0 to (Sender as TDBGrid).Columns.Count-1 do
begin
(Sender as TDBGrid).Columns[i].Title.Font.Name :='宋体'; //字体
(Sender as TDBGrid).Columns[i].Title.Font.Size :=9; //字体大小
(Sender as TDBGrid).Columns[i].Title.Font.Color :=$000000ff; //字体颜色(红色)
(Sender as TDBGrid).Columns[i].Title.Color :=$0000ff00; //背景色(绿色)
end;
//隔行改变网格背景色:
if DM.cMyQry1.RecNo mod 2 = 0 then
(Sender as TDBGrid).Canvas.Brush.Color := clInfoBk //定义背景颜色
else
(Sender as TDBGrid).Canvas.Brush.Color := RGB(191, 255, 223); //定义背景颜色
//定义网格线的颜色:
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
with (Sender as TDBGrid).Canvas do //画 cell 的边框
begin
Pen.Color := $00ff0000; //定义画笔颜色(蓝色)
MoveTo(Rect.Left, Rect.Bottom); //画笔定位
LineTo(Rect.Right, Rect.Bottom); //画蓝色的横线
Pen.Color := $0000ff00; //定义画笔颜色(绿色)
MoveTo(Rect.Right, Rect.Top); //画笔定位
LineTo(Rect.Right, Rect.Bottom); //画绿色的竖线
end;
end;
// OnDrawColumnCell事件
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
with DBGrid1 do
with Canvas do begin
if TMyGrid(DBGrid1).Row = MouseCoord(Rect.Left+1, Rect.Top+1).Y then
Canvas.Brush.Color := clRed;
DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
end;
Var
p : Integer;
begin
p := Table1.FindField('wage').AsInteger;
//取得当前记录的Wage字段的值。
if(p < 500) then begin
//程序将根据wage值设置各行的颜色。
Color := clGreen;
Font.Style := [fsItalic];
//不仅可以改变颜色,还可以改变字体
end;
if (p >= 500) And (p < 800) then
Color := clRed;
if(p >=800) then begin
Color := clMaroon;
Font.Style := [fsBold];
end;
end;