Using Delphi OnDrawColumnCell how can I ensure record value exists prior to applying conditional format

1.8k views Asked by At

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.

2

There are 2 answers

1
Marjan Venema On

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);
0
Ken White On

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;