使用Delphi OnDrawColumnCell如何在应用条件格式之前确保记录值存在

使用Delphi OnDrawColumnCell如何在应用条件格式之前确保记录值存在,delphi,dbgrid,Delphi,Dbgrid,我使用Delphi2010,通过ADO将dbgrid绑定到Access mdb数据库中的表中 此表根据无线组框中的单击进行过滤 以下代码根据表中的数据对行进行颜色编码,但失败,并显示一个错误对话框 "" is not a valid integer value 如果筛选器返回空数据集。我想我已经允许在没有返回任何记录但它似乎不起作用时不调用颜色设置;请参阅下面的代码 procedure TMainForm.DBGridAbsenceDrawColumnCell(Sender: T

我使用Delphi2010,通过ADO将dbgrid绑定到Access mdb数据库中的表中

此表根据无线组框中的单击进行过滤

以下代码根据表中的数据对行进行颜色编码,但失败,并显示一个错误对话框

       "" is not a valid integer value
如果筛选器返回空数据集。我想我已经允许在没有返回任何记录但它似乎不起作用时不调用颜色设置;请参阅下面的代码

procedure TMainForm.DBGridAbsenceDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
     //Need to add code to detect if record exists
     with DBGridAbsence.Canvas do
     begin
           RecordCountLabel.Caption.Text :=  'Absence Records: ' + IntToStr(ADOTblAbsence.RecordCount);
           font.color:=clBlack;
           brush.color:=clMoneyGreen;
           If ( ADOTblAbsence.State <> dsInsert ) and ( ADOTblAbsence.RecordCount > 0 ) then
           begin
                Font.Color := StringToColor(ADOTblAbsenceForeground.AsString);
                Brush.Color:= StringToColor(ADOTblAbsenceBackground.asstring);
           end;

          DBGridAbsence.DefaultDrawColumnCell(Rect, DataCol, Column, State);
    end;
end;
过程tmaninform.dbgridAbscenceDrawColumnCell(发送方:toObject;
const Rect:TRect;DataCol:Integer;Column:TColumn;State:TGridDrawState);
开始
//需要添加代码以检测记录是否存在
使用dbgrididence.Canvas do
开始
RecordCountLabel.Caption.Text:=“缺勤记录:”+IntToStr(adotLabsence.RecordCount);
字体.颜色:=clBlack;
画笔颜色:=绿色;
如果(ADOTBLASENCE.State dsInsert)和(ADOTBLASENCE.RecordCount>0),则
开始
Font.Color:=StringToColor(ADOTBLASENCEFORGROUND.AsString);
Brush.Color:=StringToColor(adotlabsencebackground.asstring);
结束;
DefaultDrawColumnCell(Rect、DataCol、Column、State);
结束;
结束;
感谢所有的帮助。注意,我还尝试了条件句,例如

if Trim(ADOTblAbsenceForeground.AsString) <> ''
如果修剪(ADOTBLASENCEFORGROUND.AsString)'
但这似乎也不起作用。

当您试图将空字符串转换为整数时,会出现“”不是有效整数值的消息。您的代码示例中似乎没有任何相关内容。所以要么是在你从这里调用的代码中,你没有向我们展示过,要么是在完全其他的代码中

StringToColor可能是一个候选颜色。如果这些列中的值为空,则会触发该消息。因此,请使用表浏览器或数据库管理器软件检查db值

另外,在执行strotint之前,您也可以使用strotintdef,它使用try-except块包围strotint,并在异常发生时返回您提供的默认值,而不是检查所有这些不同的场景

SomeInt := StrToIntDef(SomeString, 0);

我将在过程开始时测试数据集是否为空:

procedure TMainForm.DBGridAbsenceDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  if not DBGridAbsensce.DataSet.IsEmpty then
  begin
    //Need to add code to detect if record exists
    with DBGridAbsence.Canvas do
    begin
      RecordCountLabel.Caption.Text :=  'Absence Records: ' +
               IntToStr(ADOTblAbsence.RecordCount);
      font.color:=clBlack;
      brush.color:=clMoneyGreen;
      If ( ADOTblAbsence.State <> dsInsert ) then
      begin
        Font.Color := StringToColor(ADOTblAbsenceForeground.AsString);
        Brush.Color:= StringToColor(ADOTblAbsenceBackground.asstring);
      end;
    end;
  end;
  DBGridAbsence.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
过程tmaninform.dbgridAbscenceDrawColumnCell(发送方:toObject;
const Rect:TRect;DataCol:Integer;Column:TColumn;State:TGridDrawState);
开始
如果不是DBGridAbsensce.DataSet.IsEmpty,则
开始
//需要添加代码以检测记录是否存在
使用dbgrididence.Canvas do
开始
RecordCountLabel.Caption.Text:=“缺勤记录:”+
IntToStr(ADOTBLASENCE.RecordCount);
字体.颜色:=clBlack;
画笔颜色:=绿色;
如果(ADOTBLASENCE.State dsInsert),则
开始
Font.Color:=StringToColor(ADOTBLASENCEFORGROUND.AsString);
Brush.Color:=StringToColor(adotlabsencebackground.asstring);
结束;
结束;
结束;
DefaultDrawColumnCell(Rect、DataCol、Column、State);
结束;
您的代码示例应该是strointdef而不是stroint。我自己无法编辑,因为它只有三个字符。