如何随意控制DBGrid每一行颜色(一整行,并不是某一字段),且颜色随光标变化!在线等待。谢谢! (50分)

  • 主题发起人 主题发起人 20020509
  • 开始时间 开始时间
2

20020509

Unregistered / Unconfirmed
GUEST, unregistred user!
如何随意控制DBGrid每一行颜色(一整行,并不是某一字段),且颜色随光标变化!在线等待。谢谢!
昨天有位仁兄给我的答案是控制某一字段,不管怎样首先谢谢了。

 
数据量不大可以改用 StringGrid
 
参考如下代码:
procedure TForm1.DBGridEh2DrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumnEh;
State: TGridDrawState);
begin
if (Rect.Top = DBGridEh2.CellRect(DBGridEh2.Col,DBGridEh2.Row).Top) and (not (gdFocused in State) or not DBGridEh2.Focused) then
DBGridEh2.Canvas.Brush.Color := clAqua;
DBGridEh2.DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;
 
其实这里的关键代码是:
DbGrid.datasource.DataSet.RecNo
///////////////////////////////

给你一个示例,自己去体会,其它的你应该会:

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
Grid:TDBGrid;
R:TRect;
begin

Grid:=TDbGrid(Sender);
r.left:=Rect.left-1;
r.Top:=Rect.Top-1;
r.right:=Rect.right+1;
r.bottom:=Rect.bottom+1;
with Grid do
begin
if (datasource.DataSet.RecNo Mod 2)=0
then
canvas.Brush.color:=clYellow
else
canvas.Brush.color:=$00ff00;
Font.color:=clRed;
canvas.Rectangle(r);
canvas.TextOut(rect.left,rect.top,column.Field.AsString );
end;
end;

 
不行啊DBGrid1DrawColumnCell事件中Datasource.DataSet.RecNo总是-1啊
 
一个表示碟子好坏状态的例子:
procedure Tmvcd.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if query1['好坏状态']='坏' then
begin
dbgrid1.Canvas.Font.Color:=clwhite;
dbgrid1.Canvas.Brush.Color:=clred;
dbgrid1.DefaultDrawColumnCell(rect,datacol,column,state);
end;
if (query1['好坏状态']<>'坏') and (query1['碟片产地']='国产片') then
begin
dbgrid1.Canvas.Font.Color:=clblue;
dbgrid1.Canvas.Brush.Color:=clskyblue;
dbgrid1.DefaultDrawColumnCell(rect,datacol,column,state);
end;
end;
 
还是建议使用TdxDBGrid,控制非常方便,www.51delphi.com就有。
 
可以参考一下,在TDBGrid的OnDrawColumnCell事件中添加下面的代码。

procedure TBaseBrowseForm.DBGrid1DrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
begin
with (Sender as TDBGrid).Canvas do
begin
// if DataCol = ? then Font.Color := cl? //可以不同字段用不同字体
//DBGrid1.SelectedRows.CurrentRowSelected:=True;
// if (Sender as TDBGrid).DataSource.DataSet.RecNo<>-1 then
begin
if (gdSelected in State) then
//如果是被选中的
begin
Brush.Color := clActiveCaption;
Font.Color := clWhite;
end
else
begin
Font.Color := clBlack;
if (Sender as TDBGrid).DataSource.DataSet.RecNo mod 2 = 1 then
Brush.Color := clWhite
else
Brush.Color := $00E7FFE7;
end;
end;
end;
(Sender as TDBGrid).DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
 
如果字段有唯一标识还可以
nid:integer;
procedure TForm1.DBGridEh2DrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumnEh;
State: TGridDrawState);
begin
if Query1.fieldbyname('id').asinteger:=nid;
then
DBGridEh2.Canvas.Brush.Color := clAqua;
DBGridEh2.DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;
afterscroll事件:
nid:=query1.fieldbyname('id').asinteger;
dbgrid1.refresh;
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
748
import
I
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部