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

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

20020509

Unregistered / Unconfirmed
GUEST, unregistred user!
如何随意控制DBGrid每一行颜色,且颜色随光标变化!在线等待。谢谢!
 
我也在考虑这个问题,但不知DBGrid有没有这方面的功能。
 
afterscroll 取得id,invalidate
ondrawcolumn if fieldbyname('id') then 颜色
procedure TQryResult_F.DBGrid2DrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
begin
if qrypara.fieldbyname('dot_time').asinteger=ndot_time then
begin
dbgrid2.Canvas.Font.Color:=clblue;
dbgrid2.DefaultDrawColumnCell(rect,datacol,column,state);
end else
begin
dbgrid2.Canvas.Font.Color:=clblack;
dbgrid2.DefaultDrawColumnCell(rect,datacol,column,state);
end;


end;

procedure TQryResult_F.QryparaAfterScroll(DataSet: TDataSet);
begin
ndot_time:=qrypara.fieldbyname('dot_time').asinteger;
dbgrid2.Invalidate;
end;
 
控制行的颜色是在DrawColumnCell中实现的:
with DBGrid1 do
begin
if ((State = [gdSelected]) or (State = [gdSelected, gdFocused])) then
begin
Canvas.Font.Color := ClYellow;
Canvas.Brush.Color := ClNavy;
end
else
begin
if DBGrid1.DataSource.DataSet.RecNo mod 2 <> 0 then
{判断当前数据是奇数还是偶数行}
Canvas.brush.Color := ClWhite {如果是奇数行,DBGrid背景以白色显示}
else
Canvas.brush.Color := $00EAEAEA; {如果是偶数行,DBGrid背景以浅灰色显示}
end;
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;

 
来晚了~~~~~~~~~~
 
多人接受答案了。
 
DbGrid.datasource.DataSet.RecNo始终为-1啊,应该如何办呢?
 

Similar threads

I
回复
0
查看
748
import
I
I
回复
0
查看
592
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
后退
顶部