1

我正在使用带有通过 ADO 绑定到 Access mdb 数据库中的表的 dbgrid 的 Delphi 2010。

此表是根据单选框中的点击进行过滤的。

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

       "" 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;

任何和所有的帮助都非常感激。注意我也尝试过条件,例如

if Trim(ADOTblAbsenceForeground.AsString) <> ''

但这似乎也不起作用。

4

2 回答 2

2

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

StringToColor 可能是候选对象。如果这些列中有空值,这就是该消息被触发的方式。因此,请使用表格浏览器或数据库管理器软件检查您的 db 值。

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

SomeInt := StrToIntDef(SomeString, 0);
于 2011-02-24T14:45:00.433 回答
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;
于 2011-02-24T14:47:45.413 回答