又是关于dbgrid的问题(50分)

  • 主题发起人 主题发起人 delphilike
  • 开始时间 开始时间
D

delphilike

Unregistered / Unconfirmed
GUEST, unregistred user!
我知道可以动态设置dbgrid中的字段颜色,如下程序
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect:TRect;DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if Table1.FieldByName('Population').AsInteger > 20000000 then
DBGrid1.Canvas.Font.Color := clBlue;
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
想请问如何实现改变当前记录的前景和背景
以区分是当前记录呢?
首先应该判断是否当前记录吧
 
DBGRID有一个字段表示当前字段,这个字段我试过,好像没有用.
不知仁兄怎么解决这个问题,如果有好消息请mail我:xueminliu@263.net,谢谢
 
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
begin
if (Rect.Top = TStringGrid(DBGrid1).CellRect(TStringGrid(DBGrid1).col,TStringGrid(DBGrid1).row).top) and (not (gdFocused in State)) then
Dbgrid1.Canvas.Brush.Color := clRed;
DBGrid1.DefaultDrawDataCell(Rect, Field, State);
end;
 
if (gdFocused in State) then
Dbgrid1.Canvas.Brush.Color := clRed;
 
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if gdfocused in State then//or gdselected
with DBGrid1.Canvas do
begin
Brush.Color:=clred;//背景色
Font.Color:=clWhite;//字体颜色
DBGrid1.DefaultDrawDataCell(Rect,Column.Field,State);
end;
end;
 
zhshf你说的不错,但是只是能使当前列变色啊
怎样才能使当前记录整个变颜色呢?
请赐教
 
zhshf写的是对的,在《Delphi编程技术内幕》中关于改变网格里一行的颜色就是用
DbGrid.canvas.brush.color 和 DbGrid.canvas.font.color实现的。程序如下:

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if (IsTagged(Column)) then begin
DBGrid1.Canvas.Brush.Color := clPurple;
DBGrid1.Canvas.FillRect(Rect);
end else if (ColorRows1.Checked) then begin
if (DMod.Query1ItemsTotal.Value < 1000) then
DBGrid1.Canvas.Font.Color := clRed
else if (DMod.Query1ItemsTotal.Value < 10000) then
DBGrid1.Canvas.Font.Color := clBlue
else
DBGrid1.Canvas.Font.Color := clGreen;
end;
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
数据对象的值小于1000,记录是红色,小于10000是蓝色,否则是绿色。
 
将DBGrid的Options中的dgRowSelect设为True;
用以下的过程即可!其实这些语句写在DrawDataCell和DrawColumnCell中都可以,只有最后一句不一样。
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
begin
if gdselected in State then//必须用gdselected
with DBGrid1.Canvas do
begin
Brush.Color:=clred;//背景色
Font.Color:=clWhite;//字体颜色
DBGrid1.DefaultDrawDataCell(Rect,Field,State);
//如果在DrawColumnCell事件中,用下面的语句。
//DBGrid1.DefaultDrawDataCell(Rect,Column.Field,State);
end;
end;
 
zhshf的方法应该可以行的,我也曾经用过!
 
多人接受答案了。
 
后退
顶部