当我运行这段代码时,出现错误消息"Type mismatch in expression":
CDSIndicados.Filtered := False;
CDSIndicados.Filter := 'EDICOES_ID like ' + QuotedStr(IntToStr(Integer(cxComboBox1.Properties.Items.Objects[cxComboBox1.ItemIndex])));
CDSIndicados.Filtered := True;
我知道当字段的数据类型有错误时可能会出现此消息。但我无法修复。是这样吗?
我怀疑您的EDICOES_ID
字段是整数值,在这种情况下,您不需要在过滤器表达式中引用它,并且LIKE
操作符不支持AFAIK。如果它是一个字符串字段,您确实需要引号并且支持LIKE
,但您通常也希望在表达式中使用通配符。(LIKE仅支持字符(字符串)类型字段。对于数字或日期,需要使用常用的比较操作符>、<、>=、<=、=或BETWEEN)
也帮你自己一个忙,声明一个局部变量,并确保在试图访问Objects
之前,ComboBox
中确实选择了一个项目。我已经为ItemIndex
和您正在检索的类型转换Object
的中间存储添加了一个,如果需要的话,这使得调试变得容易得多。
这里有一个解决方案(无论是整数字段,还是需要引号的字符串)。
var
Idx, Value: Integer;
begin
Idx := ComboBox1.ItemIndex;
if Idx > -1 then
begin
CDSIndicados.Filtered := False;
Value := Integer(cxComboBox1.Properties.Items.Objects[Idx]);
// If the field is an integer, you don't need a quoted value,
// and LIKE isn't supported in the filter.
CDSIndicados.Filter := 'EDICOES_ID = ' + IntToStr(Value);
// Not relevant here, but LIKE isn't supported for date values
// either. For those, use something like this
CDSIndicados.Filter := 'EDICOES_DATE = ' + QuotedStr(DateToStr(Value));
// or, if the field is string and you want LIKE, you need to
// quote the value and include a wildcard inside that quoted
// string.
CDSIndicados.Filter := 'EDICOES_ID LIKE ' + QuotedStr(IntToStr(Value) + '%');
CDSIndicados.Filtered := True;
end;
end;