使用 Delphi OnDrawColumnCell 如何确保在应用条件格式之前记录值存在
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您尝试将空字符串转换为整数时,会出现“”不是有效的整数值消息。您的代码示例中没有任何内容看起来相关。因此,它要么必须在您从此处调用但尚未向我们展示的某些代码中,要么在某些完全其他的代码中。
StringToColor 可能是一个候选者。如果这些列中有空值,则会触发该消息。因此,请使用表浏览器或数据库管理器软件检查您的数据库值。
此外,您还可以使用 StrToIntDef,而不是在执行 StrToInt 之前检查所有这些不同的场景,它用 try except 块包围 StrToInt,并在发生异常时返回您提供的默认值;
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;
我首先测试数据集在过程开始时是否为空:
I'd start by a test for the dataset being empty at the beginning of the procedure: