test := TFDMemTable.Create(nil);
test.CopyDataSet(TempUnplannedDemand, [coStructure, coRestart, coAppend]);
test.First;
while not test.Eof do
begin
ShowMessage(DateTimeToStr(test.FieldByName('demand_date').AsDateTime) +
' - ' +
IntToStr(test.FieldByName('level').AsInteger));
test.Next;
end;
Run Code Online (Sandbox Code Playgroud)
将显示
24/03/2017 - 1
24/03/2017 - 0
24/03/2017 - 1
24/03/2017 - 1
test.IndexFieldNames := 'level';
//test.SetRangeStart;
//test.FieldByName('level').AsInteger := 0;
//test.SetRangeEnd;
//test.FieldByName('level').AsInteger := 0;
//test.ApplyRange;
//test.SetRange([0],[0]);
test.Filter := 'level=0';
test.Filtered := True;
test.First;
while not test.Eof do
begin
ShowMessage(DateTimeToStr(test.FieldByName('demand_date').AsDateTime) +
' - ' +
IntToStr(test.FieldByName('level').AsInteger));
test.Next;
end;
Run Code Online (Sandbox Code Playgroud)
将显示
24/03/2017 - 1
24/03/2017 - 0
24/03/2017 - 1
24/03/2017 - 1
为什么test.Filter:='level = 0'; 不工作
test.Filter:='level = 0'对不起的结果是RecCount = 0.
test.Filter:='level = 1'=> RecCount = 3
test.Filter:='level <> 1'=> RecCount = 1
我认为你的问题必须在于你没有包含在你的q中.以下工作正常并产生Level = 0和Level = 1的预期结果.Test数据集的字段ID = ftInteger,Demand_Date = ftDateTime和Level = ftInteger并TDBGrid通过以下方式连接到a TDataSource:
procedure TForm1.FormCreate(Sender: TObject);
begin
Test.CreateDataSet;
Test.InsertRecord([1, '24/03/2017', 0]);
Test.InsertRecord([2, '24/03/2017', 1]);
Test.InsertRecord([3, '24/03/2017', 0]);
Test.InsertRecord([4, '24/03/2017', 1]);
Test.InsertRecord([5, '24/03/2017']); // <- this leaves the Level column as Null
Test.IndexFieldNames := 'Level';
end;
procedure TForm1.ApplyFilter;
begin
Test.Filtered := False;
Test.Filter := 'Level=0'; // or 'Level=1'
Test.Filtered := True;
end;
Run Code Online (Sandbox Code Playgroud)
注意ID = 5的数据行; 这不包含Level列的值,FireDAC因此将其视为包含Null值,因此,该行将不会包含在已过滤的重新设置中,无论Level是否指定为1或0,因为Null不匹配这些.
顺便说一下,一个空的ftInteger列将返回0作为其AsInteger值,因此将返回一个ftString值.
| 归档时间: |
|
| 查看次数: |
613 次 |
| 最近记录: |