procedure TDCMain.DBGrid2DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); begin if dbgrid2.DataSource.DataSet.RecordCount=0 then begin exit;end; dbgrid2.DataSource.DataSet.First; while (not dbgrid2.DataSource.DataSet.Eof) do begin if (dbgrid2.DataSource.DataSet.FieldByName('starttime').AsString>formatdatetime('yyyy-mm-dd hh:mm:ss',now+0.125)) and (dbgrid2.DataSource.DataSet.FieldByName('starttime').AsString<formatdatetime('yyyy-mm-dd hh:mm:ss',now+0.1667)) then begin dbgrid2.Canvas.Brush.Color:=clblue; dbgrid2.Canvas.FillRect(Rect);end; dbgrid2.DataSource.DataSet.Next; end; end; //每次启动程序时这段代码不断执行,而把代码放到Drawdatacell里时又不要执行,快给我解答解答吧。
把while语句前面的都去掉就行了,这个地方程序自己会循环的
呵呵,你真有命,刚刚做一个成绩管理系统,应要有这样的功能,如:不及格就要变成红色对吗?你看这个代码有用吗? procedure Txscjs.xscjgridDrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); var value:string; begin value:=Query.Fields.Fields[index].AsString得到分数 begin if strtoint(value)<60 then begin if datacol=2 then begin xscjgrid.Canvas.Font.Color:=clred; xscjGrid.DefaultDrawDataCell(Rect, Column.Field, State); end; end;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if dbgrid2.DataSource.DataSet.RecordCount=0 then begin exit;end;
dbgrid2.DataSource.DataSet.First;
while (not dbgrid2.DataSource.DataSet.Eof) do begin
if (dbgrid2.DataSource.DataSet.FieldByName('starttime').AsString>formatdatetime('yyyy-mm-dd hh:mm:ss',now+0.125)) and (dbgrid2.DataSource.DataSet.FieldByName('starttime').AsString<formatdatetime('yyyy-mm-dd hh:mm:ss',now+0.1667)) then begin
dbgrid2.Canvas.Brush.Color:=clblue;
dbgrid2.Canvas.FillRect(Rect);end;
dbgrid2.DataSource.DataSet.Next; end;
end;
//每次启动程序时这段代码不断执行,而把代码放到Drawdatacell里时又不要执行,快给我解答解答吧。
procedure Txscjs.xscjgridDrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
value:string;
begin
value:=Query.Fields.Fields[index].AsString得到分数
begin
if strtoint(value)<60 then
begin
if datacol=2 then
begin
xscjgrid.Canvas.Font.Color:=clred;
xscjGrid.DefaultDrawDataCell(Rect, Column.Field, State);
end;
end;
haerbin982:多谢你的代码,可这段代码也是不停执行,,没几下就溢出错误了。
你把dbgrid删了,再重加一个试试!
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
TempBlob: TBlobField;
Picture:TPicture;
begin
if Column.FieldName = 'FID' then
begin
DBGrid1.Canvas.TextRect(Rect,Rect.Left,Rect.Top,'Hello, world!');
DBGrid1.Canvas.Brush.Color := clBlue;
DBGrid1.DefaultDrawDataCell(Rect,Column.Field,State);
end
else if Column.FieldName = 'FPICTURE' then
begin
try
Picture := TPicture.Create;
TempBlob := TBlobField(ADOTable1.FieldByName('fpicture'));
TempBlob.SaveToFile('D:\TempMap.jpg');
Picture.LoadFromFile('D:\TempMap.jpg');
DBGrid1.Canvas.StretchDraw(Rect,Picture.Graphic);
DeleteFile('D:\TempMap.jpg');
finally
Picture.Free;
end;
end;
end;
说明几句:要执行该事件,首先在DGGrid中添加字段名,然后可对对应的列进行处理,当然你可以根据不同的条件来设置。