【发布时间】:2012-07-28 13:58:37
【问题描述】:
以下代码将根据在TEdit 控件中键入的内容过滤TListView 控件的项目,如果 ListView 由单列组成,它可以正常工作,但是如果您有超过 1 列,然后在应用过滤器时其他列中的项目被破坏,所以我希望有人可能知道需要在下面的代码中添加什么以在过滤 ListView 时保留这些列。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, strutils, StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
ListView1: TListView;
Edit1: TEdit;
procedure Edit1Change(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
StrList : TStringList;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
Index : Integer;
begin
StrList := TStringList.Create;
for Index := 0 to ListView1.Items.Count -1 do
StrList.Add(ListView1.Items[Index].Caption);
end;
procedure TForm1.Edit1Change(Sender: TObject);
var
Index : Integer;
begin
ListView1.Clear;
for Index := 0 to StrList.Count - 1 do
if Pos(Edit1.Text, StrList.Strings[Index]) > 0 then
ListView1.AddItem(StrList.Strings[Index], nil);
if Edit1.Text = '' then
for Index := 0 to StrList.Count - 1 do
ListView1.AddItem(StrList.Strings[Index], nil);
end;
end.
【问题讨论】:
-
听起来不像想要一个行过滤器,因为它显然包含或排除了该行。如果它不匹配,看起来你只想在单元格中粘贴一个空白值。如果没有列匹配,也许不要添加行...
-
或者也许你可以测试每一列,如果其中任何一个匹配添加行,他想了一会儿说。更好的是,您将行中的数据和过滤器传递给布尔函数,如果为真则添加,这将是一种更自然的方法。
-
抱歉,我不太听从您的回复。请您再次尝试改写一下。感谢您的回复顺便说一句。我要做的就是创建一个过滤器,就像您使用 DBgrid 一样,但我只是使用 ListView 和编辑控件,而不是带有 Dataset 的网格。
-
嗯?您说“其他列中的项目被破坏”。当然是,您过滤掉了行,这也是 DBGrid 过滤器的工作原理。