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

发布于 2024-10-19 13:54:10 字数 1233 浏览 10 评论 0原文

我正在使用 Delphi 2010,其中 dbgrid 通过 ADO 绑定到 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;

非常感谢您提供的所有帮助。注意我也尝试过条件语句,例如,

if Trim(ADOTblAbsenceForeground.AsString) <> ''

但这似乎也不起作用。

I am using Delphi 2010 with a dbgrid bound via ADO to a table in an Access mdb Database.

This table is filtered based on clicks in a radio group box.

The following code colour codes the rows based on the data in the table, but fails with an error dialogue box stating

       "" is not a valid integer value

if the filter returns a null dataset. I thought I had allowed for not invoking colour setting if no records returned but it appears not to work ; see code below

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;

Any and all help gratefully recieved. Note I have also tried conditionals such as

if Trim(ADOTblAbsenceForeground.AsString) <> ''

but this does not appear to work either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花期渐远 2024-10-26 13:54:10

当您尝试将空字符串转换为整数时,会出现“”不是有效的整数值消息。您的代码示例中没有任何内容看起来相关。因此,它要么必须在您从此处调用但尚未向我们展示的某些代码中,要么在某些完全其他的代码中。

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

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

SomeInt := StrToIntDef(SomeString, 0);

The "" is not a valid integer value message occurs when you are trying to convert an empty string to an integer. There is nothing in your code sample that seems related. So either it must be in some code you are calling from here that you haven't shown us, or in some completely other code.

The StringToColor might be a candidate. If you have empty values in those columns, that's how that message gets triggered. So check your db values with a table browser or your database manager software.

Also, instead of checking all these various scenario's before doing a StrToInt, you could also use StrToIntDef which surrounds the StrToInt with a try except block and returns the default you supply when an exception occurs;

SomeInt := StrToIntDef(SomeString, 0);
懷念過去 2024-10-26 13:54:10

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

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;

I'd start by a test for the dataset being empty at the beginning of the procedure:

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文