大家帮忙看看,关于stringgrid的问题(20分)

  • 主题发起人 主题发起人 cnbobo
  • 开始时间 开始时间
C

cnbobo

Unregistered / Unconfirmed
GUEST, unregistred user!
我的数据库有一个“状态”这一字段,现在想在状态=1时stringgrid的一个网格变红色。

procedure TForm3.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var i,j:integer;
k:string;
begin
//form1.ADOQuery1.Close;
//form1.ADOQuery1.SQL.Clear;
//form1.ADOQuery1.SQL.Add('select * from 包厢 ');
//form1.ADOQuery1.Open;
form1.ADOQuery1.First;
for j:=0 to form3.stringgrid1.rowcount-1 do
begin
for i:=0 to form3.stringgrid1.ColCount-1 do
begin
k:=form1.ADOQuery1.fieldbyname('状态').AsString;
if(ARow=j)and(ACol=i)then
begin
if k='1' then
begin
StringGrid1.Canvas.Brush.Color:=clRed;
StringGrid1.Canvas.FillRect(Rect); //画红色
StringGrid1.OnDrawCell:=nil; //设置为nil,下面就不会无限递归
try
TMyGrid(StringGrid1).DrawCell(ACol,ARow,Rect,State);
finally
StringGrid1.OnDrawCell:=StringGrid1DrawCell; //恢复事件的地址
end;
end;
end;
end;
end;
end;
 
为何没人帮我,晕~
 
if k='1' then
begin
StringGrid1.Canvas.Brush.Color:=clRed;
StringGrid1.DefaultDrawCell(ACol,ARow,Rect,State);
end;
 
和和,不用那么麻烦的。
因为画StringGrid的每个格子的时候都会调用那个函数
所以,你只需要按楼上的说的做,就可以了
还有要把stringgrid.ownerdraw设为True
另外,当状态为1时,画的结果是所有的格子都是红色,不知道是不是合你要求
如果你只想使某个格子红色,那么你必须知道是哪个格子,比如redcol,redrow.
那么
if (k='1') and (acol=redcol) and (arow=redrow) then
StringGrid1.Canvas.Brush.Color:=clRed
else
StringGrid1.Canvas.Brush.Color:=clwhite;
StringGrid1.DefaultDrawCell(ACol,ARow,Rect,State);
 
正如楼上几位所说,自己不需要遍历StringGrid的格格
Drawcell已经帮你这样做了,你只需要把格格的特征告诉该事件,
该事件自动会帮你重画,对于Delphi的网格控件就是这样的:
自己不需要遍历!!
 
to:twos
if (k='1') and (acol=redcol) and (arow=redrow) then
我现在得不到k,recol,redrow;K在数据库中。RECOL,REDROW的值怎么定,这两个值不固定
要由K来定
 
to cnbobo,
你为什么不TDBGrid?
这样在它的OnDrawDataCell事件中写几句话就完事了。
 
to cnbobo,
这是用TDBGrid的例子,

procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
begin
if Field.FieldNo=5 then
begin
if Field.AsDateTime>DateTimePicker1.DateTime then
DBGrid1.Canvas.Brush.color:=clRed
else
DBGrid1.Canvas.Brush.color:=clBlue;

DBGrid1.DefaultDrawDataCell(Rect, Field, State);
end;
end;
 
你的做法不对!!!!!!!
事件中只需要以下代码。
赋值在其它地方进行。

procedure TForm3.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var i,j:integer;
k:string;
begin
if(ARow=j)and(ACol=i)then
begin
if k='1' then
begin
StringGrid1.Canvas.Brush.Color:=clRed;
StringGrid1.Canvas.FillRect(Rect); //画红色
StringGrid1.Canvas.textout(rect.left+2,rect.top+2,stringgrid1.cells[acol,arow]);
end;
end;
end;
 
to:大家
请注意:cnbobo的问题???
[red]我现在得不到k,recol,redrow;K在数据库中。RECOL,REDROW的值怎么定,这两个值不固定
要由K来定[/red]
 
那要问cnbobo是怎样把StringGrid和数据联系起来的
只要知道这一点就会知道问题出在哪里
 
多人接受答案了。
 
后退
顶部