大侠们,有空帮忙看看(50分)

  • 主题发起人 mylemontree
  • 开始时间
M

mylemontree

Unregistered / Unconfirmed
GUEST, unregistred user!

我想在DBGRID中用不同的颜色表示事件不同的状态,如
procedure TfrmMainForm.DBGrid1DrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
begin
frmDataModule.tblCaseState.first;
while Not frmDataModule.tblCaseState.EOF DO
BEGIN
CaseState:=frmDataModule.tblCaseState.Fieldbyname('CaseState').AsInteger;

case CaseState of
0:
begin
DBGrid1.Canvas.Font.Color:=clred;
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;
1:
begin
DBGrid1.Canvas.Font.Color:=clBlue;
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;
2:
begin
DBGrid1.Canvas.Font.Color:=clgreen;
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;

END;
frmDataModule.tblCaseState.NEXT;
end;
end;
进入死循环,怎么班是不是DrawColumnCell中不能加循环语句,那么我的想法应该怎么
实现?
 
你不用循環試以下,只用 IF 判斷 ,看行不行!
我這樣用是可以的!
 
>>进入死循环,
TfrmMainForm.DBGrid1DrawColumnCell这个事件是永远不会停止的,
放到另外的事件中试试;
 
在DrawColumnCell中不能加入循环,因为它是一格一格的画,你反复的调用自然成了死循环.
在DBGrid中Column就是指的你那一个栏的值.你判断它就行了.TColumn.Field...
你用下面的方法试试,我的是StringGrid,DBGrid应该差不多.
procedure TFrmShow.StrGridShowDrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
var Area:TRect;
begin
StrGridShow.Canvas.Font.Assign(StrGridShow.Font);//一句可以放到Form的OnCreate里
with StrGridShow,StrGridShow.Canvas do
begin
FillRect(Rect);
Area:= Rect;
InflateRect(Area, -2, -2);
if (ACol<2) or (ARow=0) then DrawText(Handle, PChar(Cells[ACol, ARow]),
Length(Cells[ACol, ARow]), Area, DT_CENTER)
else DrawText(Handle, PChar(Cells[ACol, ARow]),Length(Cells[ACol, ARow]), Area, DT_RIGHT);
end;
end;
 

TO zhangkan
我把程序改成这样,
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
CaseState:=frmDataModule.tblCaseState.Fieldbyname('CaseState').AsInteger;
case CaseState of
0:
begin
DBGrid1.Canvas.Font.Color:=clred;
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;
1:
begin
DBGrid1.Canvas.Font.Color:=clBlue;
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;
2:
begin
DBGrid1.Canvas.Font.Color:=clgreen;
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;
end;

但DBGRID只是一种颜色,不是我想要得用不同色彩代表不同状态,但奇怪的是,我在另外
一个PROJECT中却实现,在我看来着两者没有不同,这到底为什么,帮忙看看好吗?


procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if Table1.FieldByName('医院编号').AsInteger>0002 then
DBGrid1.Canvas.Font.Color:=clBlue;
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
if Table1.FieldByName('医院编号').AsInteger<0002 then
DBGrid1.Canvas.Font.Color:=clred;
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;

end.
 
事件OnDBGridDrawColumnCell中,本来就对记录进行了循环处理,我看了你以上的代码
我试了一下是可以的
 
TO wbcp2000
单独时是对的,但放在我的程序中不知道被那里影响了,他好象只是反复读同一条记录
可能是那里呀!
 
你自己設斷點一步一步的看吧。
 
问题已经解决,谢谢大家的热心。
 
是怎么解決的﹖能告訴一下嗎﹖
 
to dadabox

of course!
是我自己犯了个认识上的错误,我的本意是用TABLE1中某字段控制与TABLE2关联的DBGRID2
的显示颜色,但我没有将TABLE1中记录循环,所以DBGRID2始终只显示一中颜色。
 

Similar threads

I
回复
0
查看
715
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
Z
回复
1
查看
405
007vivi
0
顶部