views:

659

answers:

2

Hi,

In Delphi 2009 :

When TListView's GroupView is Active, adding or inserting an item to a TListView always adds it to the end of the list, regardless of Index specified as param. When GroupView is set to false it adds it at the specified index. But when it is true, this behavior is not seen.

ListView2.Items.Insert(1)

The above should insert item at the sepecified index "1", but always adds 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 item @ index 0

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

Thanks & Regards, Pavan.

A: 

You may need to assign the new ListItem to a GroupIndex, assuming that you have added at least 1 Group first and given it an ID.

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

Or you can create the TListItem object first, give it the GroupID and use ListView.Items.AddItem(item, index) to add it to the ListView.

Gerard
No need for a Group. See my response, it should work without a Group.
François
+1  A: 

It might depend on other properties you changed (like SortType).
I tried with a simple text list (with ViewStyle =vsList) and it inserts at the specified index wether GroupView is set or not:

  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;
François
It seems to be not working when ViewStyle = vsReport , if I want to insert at a specified index, then do I need to switch back and forth between ViewStyles ?
Pavan
This problem occurs only when GroupView is active and the items are associated with in groups
Pavan