procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); begin if adoquery1.FieldByName('sex').AsString='男' then DBGrid1.Canvas.FColor:=clGreen else if adoquery1.FieldByName('sex').AsString;='女' then DBGrid1.Canvas.Color:=clBlue; DBGrid1.DefaultDrawColumnCell(rect,DataCol,column,state); end;
在DBGrid1DrawColumnCell事件中写: with DBGrid1 do begin Canvas.Font.Color:=clRed; Canvas.Brush.Color:=clYellow; end;
实现单击DBGrid中的一行时,该行的颜色变成其它的颜色type TMyGrid = class(TDBGrid);// 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;
procedure TfrmEhlib.dgStoreDrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); begin if Odd(aqStore.RecNo + 1) then {隔行变色} begin dgStore.Canvas.Brush.Color := clCream; dgStore.Canvas.Font.Color := clNavy; end; if aqStore.FieldByName('scount').asinteger > 80 then {附合条件行变色} dgStore.Canvas.brush.Color := clLime; if Column.FieldName = 'onshow' then {附合条件单元格变色} if Column.Field.AsBoolean = true then dgStore.Canvas.brush.Color := $00FFBB95; if State = [gdSelected, gdFocused] then {当前所选单元格变色} begin dgStore.Canvas.Brush.Color := clSkyBlue; dgStore.Canvas.Font.Color := clBlack; end; dgStore.DefaultDrawColumnCell(Rect, DataCol, Column, State); {生效} end;
在 Delphi 语言的数据库编程中,DBGrid 是显示数据的主要手段之一。但是 DBGrid 缺省的外观未免显得单调和缺乏创意。其实,我们完全可以在我们的程序中通过编程来达到美化 DBGrid 外观的目的。通过编程,我们可以改变 DBGrid 的表头、网格、网格线的前景色和背景色,以及相关的字体的大小和风格。 以下的示例程序演示了对 DBGrid 各属性的设置,使 Delphi 显示的表格就像网页中的表格一样漂亮美观。 示例程序的运行:在 Form1 上放置 DBGrid1、Query1、DataSource1 三个数据库组件,设置相关的属性,使 DBGrid1 能显示表中的数据。然后,在 DBGrid1 的 onDrawColumnCell 事件中键入以下代码,然后运行程序,就可以看到神奇的结果了。本代码在 Windows98、Delphi5.0 环境下调试通过。 procedure TMainForm.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 Query1.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; 运行效果:选中行变为蓝色 步骤1:设置dbgrid的options的dgrowselect为true. 步骤2:在dbgrid的ondrawcolumncell事件里面写上: procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); begin if gdSelected in state then dbgrid1.Canvas.Brush.Color:=clblue; dbgrid1.DefaultDrawColumnCell(rect,datacol,column,state); end; 运行效果:选中行变为蓝色.
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
begin
if adoquery1.FieldByName('sex').AsString='男' then DBGrid1.Canvas.FColor:=clGreen
else if adoquery1.FieldByName('sex').AsString;='女' then DBGrid1.Canvas.Color:=clBlue;
DBGrid1.DefaultDrawColumnCell(rect,DataCol,column,state);
end;
with DBGrid1 do
begin
Canvas.Font.Color:=clRed;
Canvas.Brush.Color:=clYellow;
end;
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;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
begin
if Odd(aqStore.RecNo + 1) then {隔行变色}
begin
dgStore.Canvas.Brush.Color := clCream;
dgStore.Canvas.Font.Color := clNavy;
end;
if aqStore.FieldByName('scount').asinteger > 80 then {附合条件行变色}
dgStore.Canvas.brush.Color := clLime;
if Column.FieldName = 'onshow' then {附合条件单元格变色}
if Column.Field.AsBoolean = true then
dgStore.Canvas.brush.Color := $00FFBB95;
if State = [gdSelected, gdFocused] then {当前所选单元格变色}
begin
dgStore.Canvas.Brush.Color := clSkyBlue;
dgStore.Canvas.Font.Color := clBlack;
end; dgStore.DefaultDrawColumnCell(Rect, DataCol, Column, State); {生效}
end;
以下的示例程序演示了对 DBGrid 各属性的设置,使 Delphi 显示的表格就像网页中的表格一样漂亮美观。
示例程序的运行:在 Form1 上放置 DBGrid1、Query1、DataSource1 三个数据库组件,设置相关的属性,使 DBGrid1 能显示表中的数据。然后,在 DBGrid1 的 onDrawColumnCell 事件中键入以下代码,然后运行程序,就可以看到神奇的结果了。本代码在 Windows98、Delphi5.0 环境下调试通过。
procedure TMainForm.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 Query1.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;
运行效果:选中行变为蓝色
步骤1:设置dbgrid的options的dgrowselect为true.
步骤2:在dbgrid的ondrawcolumncell事件里面写上:
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if gdSelected in state then
dbgrid1.Canvas.Brush.Color:=clblue;
dbgrid1.DefaultDrawColumnCell(rect,datacol,column,state);
end;
运行效果:选中行变为蓝色.
DefaultDrawColumnCell(Rect, DataCol, Column, State);
. \\\|///
\\ - - //
( @ @ )
+---------------------oOOoo-(?)ooOOo---------------------+
| |
| 欢迎访问 http://www.coderpub.com 技术论坛 |
| delphi,asp.net,C#,j2me,软件工程,企业管理 |
| http://www.coderpub.com |
| |
| Ooooo |
+-----------------------ooooO--( )---------------------+
( ) )|/
\|( (_/
\_)