Groups and items TListView are not displayed after cleaning and reloading groups and items

Update Delphi XE4 1 and Windows 8.

When I add groups and elements to the list view, they are displayed correctly. When I clear elements and groups and add them again, nothing appears. Is this not supposed behavior?

From DFM:

object lv: TListView
  Left = 24
  Top = 20
  Width = 250
  Height = 225
  Columns = <
    item
      Caption = 'Model'
      Width = 180
    end>
  GroupView = True
  ReadOnly = True
  RowSelect = True
  TabOrder = 0
  ViewStyle = vsReport
end

The code:

procedure TForm1.Button1Click(Sender: TObject);
var
  LListGroup: TListGroup;
  LListItem: TListItem;
begin
  lv.Items.Clear;
  lv.Groups.Clear;

  LListGroup := lv.Groups.Add;
  LListGroup.Header := 'Ford';

  LListItem := lv.Items.Add;
  LListItem.Caption := 'Escape';
  LListItem.GroupID := LListGroup.ID;

  LListItem := lv.Items.Add;
  LListItem.Caption := 'F150';
  LListItem.GroupID := LListGroup.ID;

  OutputDebugString(PChar(Format('lv.Groups.Count=%d', [lv.Groups.Count])));
end;

The first time you press a button, items appear and are grouped. The second time the list view is empty. If I comment on a line that clears groups, then it works, but the number of groups, all of which are not used, but one, increases each time.

+4
source share
1 answer

, ID TCollectionItem GroupID TListItem, GroupID TListGroup.

,

  LListItem.GroupID := LListGroup.ID; //here you are passing a wrong id for the group

to

  LListItem.GroupID := LListGroup.GroupID; //This is a valid assignment for the GroupID property
+6

Source: https://habr.com/ru/post/1531390/


All Articles