Hide Subitems from a Listview?

Asked

Viewed 1,293 times

2

Is there any way to hide subitems and their data from a listview?

I tried to start the code with something like this:

for I := 0 to Form1.LV1.Items.Count-1 do
      begin
            if Form1.LV1.Items[I].SubItems[5] = 'OK' then
      begin

But I don’t know how to continue to DISPLAY only SUBITEMS with TEXT "OK" ...

  • Thanks for the reply friend, I’m actually breaking my head here.... I couldn’t think of anything. Imagine the following, I have a LISTVIEW with 3 COLUMNS, in this LISTVIEW insert several records.... When the REGISTRATION is COMPLETED I use the BUTTON to update the same and put a "OK" in front. I need to have another BUTTON, when I click it, it "hide" the LOGS without the "OK" and show only the ones with the "OK" I could understand?

2 answers

2

Unfortunately there is no way to do this, at least not natively.

The idea I had to work around this was to save the data in a list of strings(StringList) in a global variable.

Updating

Do the following:

{ Declare na cláusula Uses as units StrUtils e Types }
var
  Form1: TForm1;
  StringList: TStringList;   // Variável global

{ 
   Procedimento responsável por carregar os itens salvos da StringList em um
   Listview declarado por você.
}
procedure ListViewLoadItems(Listview: TListView);
var
I, J: Integer;
Fields: TStringDynArray;  { Na seção Uses declare System.StrUtils e System.Types }
Item: TListItem;
begin
try
ListView.Clear;
for i := 0 to StringList.Count-1 do begin
    Fields := SplitString(StringList[i], #9);
    Item := Listview.Items.Add;
    Item.Caption := Fields[0];
    for j := 1 to high(Fields) do Item.SubItems.Add(Fields[j]);
end;
finally
    StringList.Free;
end;
end;

{
   Procedimento responsável por salvar os itens na StringList.
   NOTA: Só será salvo os itens em que o SubItem[4] não conter a sequência
   ""OK""

}
procedure ListViewSaveItems;

 procedure AddTextToLine(var Line: string; const Text: string);
 begin
    Line := Line + Text + #9;
 end;

 procedure MoveCompletedLineToList(const Strings: TStringList; var Line: string);
 begin
    Strings.Add(Copy(Line, 1, Length(Line)-1));
    Line := '';
 end;

Var
  Tempstr: string;
  I, J: Integer;

begin
StringList := TStringList.Create;
Tempstr := '';
try
for i := 0 to ListView1.Items.Count -1 do
if (ListView1.Items.Item[i] <> nil) and
   (not ListView1.Items.Item[i].SubItems[4].Equals('OK')) then begin
AddTextToLine(Tempstr, ListView1.Items[i].Caption);

for j := 0 to ListView1.Items[i].SubItems.Count -1 do begin
    AddTextToLine(Tempstr, ListView1.Items[i].SubItems[j]);
end;
MoveCompletedLineToList(StringList, Tempstr);
end;
except
// ....
end;
end;

That code has been adapted for your situation from of that reply in Soen. To use just put two buttons and call the procedures, see an example:

Save items:

procedure TForm1.BtnSaveitemsClick(Sender: TObject);
begin
ListViewSaveItems; // Salvará os itens
end;

Load saved items into another Listview:

procedure TForm1.BtnLoadItemsClick(Sender: TObject);
begin
if StringList = nil then exit; // Verifica se StringList é válido
ListViewLoadItems(ListView2);  // Carrega os itens salvos na *Listview2*
end;

See this illustration:

inserir a descrição da imagem aqui

By clicking the button Save items that do not contain in your Subitem 4 the word OK will be saved.

Now by clicking the button Load will be shown something like this:

inserir a descrição da imagem aqui

This should work for you.

  • i this Splitstring option does not exist in delphi7, which I can put in place ?

1

You for using a TClientDataSet in remembrance.

To hide the records you want not to appear, simply put a filter in the ClientDataSet and then on listview do .invalidate, and only load into the view the desired records through the ClientDataSet.

In this strategy you do not load all data on TListView, all data is loaded into the Cds. You download from the Cds to the ListView only those who are for viewing at that time.

Instead of Cds, you can think about using other resources, such as a TList of Objects, through it you can also make a Sort if need be and walk in the logs with memory efficiency and speed. But I tell you that the Cds is complete for you to manage the data the way you want.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.