Hide Subitems from a ListView?

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

I do not know how to continue to SHOW ONLY the SUBITEMS with the "OK" TEXT ...

    
asked by anonymous 25.04.2014 / 03:54

2 answers

2

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

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

Update

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;

This code has been adapted to your situation from that response in SOEn . To use just put two buttons and call the procedures, see an example:

Save the items:

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

Upload saved items to 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:

ByclickingontheSavebutton,theitemsthatdonotcontainSubItem4theOKwillbesaved.

NowwhenyouclicktheLoadbutton,somethinglikethiswillappear:

This should work for you.

    
25.04.2014 / 16:32
1

You for using TClientDataSet in memory.

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

In this strategy you do not load all the data in TListView , all data is loaded on the CDs. You transfer only the ones that are for viewing at the moment from the CDs to ListView .

In place of the CDs, you can think of using other features, such as% of Objects%, through it you can also do a sort if you need to and walk in the records efficiently memory and speed. But I tell you that the CDs are complete for you to manage the data in any way you like.

    
14.10.2014 / 03:07