TDataSet.Filter
和
TDataSet.Filtered
.您可以从任何您想要的地方获取输入,例如普通的
TEdit
.
由于您没有发布任何详细信息(例如您正在使用的DB控件、Delphi版本、提供变量名的任何代码或任何其他),因此这里有一个非常通用的示例可能会有所帮助。我正在调用附加到DBGrid的查询
Qry
,因为根据你发布的内容,你不知道还能叫它什么。
FilterRecordsButton
和
ClearFilterButton
是T按钮,以及
SearchEdit
是
泰迪
。可以随意使用您想要切换过滤器或从用户获得输入的任何控件。
procedure TForm1.FilterRecordsButtonClick(Sender: TObject);
begin
if SearchEdit.Text <> '' then
begin
{
The brackets around the column name are required because you've got
spaces in the name; they're also needed if your column name is a
reserved word. QuotedStr puts the necessary quote characters around
the value.
}
Qry.Filter := '[Customer Name] = ' + QuotedStr(SearchEdit.Text);
Qry.Filtered := True;
Qry.First;
FilterRecordsButton.Enabled := False;
ClearFilterButton.Enabled := True;
end;
end;
procedure TForm1.ClearFilterButtonClick(Sender: TObject);
begin
Qry.Filtered := False;
Qry.Filter := '';
Qry.First;
ClearFilterButton.Enabled := False;
FilterRecordsButton.Enabled := True;
end;
如果要处理大量的行(
SELECT * FROM MyTable
没有
WHERE
例如,返回几十万行),那么如果
Filtered
可能是不可接受的。在这种情况下,您最好只添加适当的
哪里
条款
SELECT
以及重新打开查询以仅显示相关行。当然,你不应该做
选择
没有
哪里
,所以您不需要这样做。:-)