Adding or adding an item to a TListView always adds it to the end when the GroupView is active

In Delphi 2009:

When a TListView GroupView is active, adding or adding an item to a TListView always adds it to the end of the list, regardless of the index specified as a parameter. When the GroupView is set to false, it adds it at the specified index. But when this is true, this behavior is not visible.

ListView2.Items.Insert(1)

The above should insert an element at the specified index "1", but always add it to the end of the list. What am I doing wrong here?

object ListView2: TListView
Left = 32
Top = 40
Width = 161
Height = 233
BorderWidth = 5
Columns = <
  item
    AutoSize = True
  end>
DoubleBuffered = False
FlatScrollBars = True
Groups = <
  item
    Header = 'test'
    Footer = 'aksdlkajsd;flkj'
    GroupID = 0
    State = [lgsNormal]
    HeaderAlign = taLeftJustify
    FooterAlign = taLeftJustify
    Subtitle = 'adgasdfasdf'
    TopDescription = 'test desc'
    BottomDescription = 'adsfasdfasdf'
    TitleImage = 0
    ExtendedImage = 0
  end
  item
    Header = 'test1'
    GroupID = 1
    State = [lgsNormal]
    HeaderAlign = taLeftJustify
    FooterAlign = taLeftJustify
    TopDescription = 'test1 desc'
    TitleImage = 1
    ExtendedImage = 1
  end>
HideSelection = False
IconOptions.WrapText = False
Items.ItemData = {
  03D80000000500000000000000FFFFFFFFFFFFFFFF0000000000000000000000
  0003740077006F00FFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000000
  086100730064006600610073006400660000000000FFFFFFFFFFFFFFFF000000
  000000000000000000057400680072006500650000000000FFFFFFFFFFFFFFFF
  000000000000000000000000036F006E00650000000000FFFFFFFFFFFFFFFF00
  00000000000000000000001866006F0075007200320033003300330033003300
  33003300330033003300330033003300330033003300330033003300}
MultiSelect = True
GroupView = True
ParentDoubleBuffered = False
ShowColumnHeaders = False
TabOrder = 0
ViewStyle = vsReport

end

and code to add @index element 0

procedure TForm1.Button1Click(Sender: TObject);
var
  oListItem: TListItem;
begin
  oListItem := ListView2.Items.Insert(0);
  oListItem.Caption := 'CCCCCCCC';
  oListItem.GroupID := 0;
end;

Thank you and respect, Pavan.

+4
source share
4 answers

, (, SortType).
( ViewStyle = vsList), . GroupView :

  object ListView1: TListView
    Left = 24
    Top = 16
    Width = 250
    Height = 150
    Columns = <>
    Items.ItemData = {
      03480000000200000000000000FFFFFFFFFFFFFFFF00000000FFFFFFFF000000
      00057400650073007400310000000000FFFFFFFFFFFFFFFF00000000FFFFFFFF
      000000000574006500730074003200}
    GroupView = True
    TabOrder = 0
    ViewStyle = vsList
  end

procedure TForm1.Button1Click(Sender: TObject);
begin
  ListView1.Items.Insert(1).Caption := Edit1.Text;
end;
+1

ListItem GroupIndex, , 1 .

var 
  item:  TListItem;
begin
  item:= ListView.Items.Add;
  item.GroupID=0;
end;

TListItem, GroupID ListView.Items.AddItem(item, index), ListView.

0

Delphi XE. , -, , Delphi COM COM-. , Items , .

, TListView .

procedure RefreshListView(const ListView: TListView);
  var ListItem : TListItem;
      List : TList<TPair<String,Boolean>>;
      Pair : TPair<String,Boolean>;
begin
  List := TList<TPair<string,Boolean>>.Create;
  try
    ListView.Items.BeginUpdate;
    try
      //To get the sorting to work right in the listview with GridView and vsReport
      //You have to rebuild the list completely
      for ListItem in ListView.Items do
      begin
        List.Add(TPair<String,Boolean>.Create(ListItem.Caption,ListItem.Selected));
      end;

      ListView.Items.Clear;

      for Pair in List do
      begin
        with ListView.Items.Add do
        begin
          Caption := Pair.Key;
          Selected := Pair.Value;
        end;
      end;
    finally
      ListView.Items.EndUpdate;
    end;
  finally
    List.Free;
  end;
end;

This is not the best solution, but it seems to work (this code was written in Delphi XE, but should work in Delphi 2009+).

0
source

BeginUpdate / EndUpdate will help to avoid this. This code should work fine

procedure TForm1.Button1Click(Sender: TObject);
var
  oListItem: TListItem;
begin
 ListView2.Items.BeginUpdate;
 oListItem := ListView2.Items.Insert(0);
 oListItem.Caption := 'CCCCCCCC';
 oListItem.GroupID := 0;
 ListView2.Items.EndUpdate;
end;
0
source

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


All Articles