Search code examples
delphiblank-linetlistview

ListView showing blank items inside a group


I've created a form to show errors in an importation of data with a ListView, the group is being created correctly, but even showing that have itens, they are "Invisible".

This is how i'm populating my ListView:

    class procedure TFrm_Erros.InternalShowErros(ATitle: string; AMessageErrorList: TMessageList);
    var
      vMessage: TMessage;
      i: integer;
      vGroups: TStringList;
      vListGroup: TListGroup;
    begin
      vGroups := TStringList.Create;
      Frm_Erros := TFrm_Erros.Create(nil);
      Frm_Erros.lvErrors.Items.BeginUpdate;
      try
        TFrm_Erros.SetTitle(ATitle);
        Frm_Erros.lvErrors.Items.Clear;
        Frm_Erros.lvErrors.Groups.Clear;

        vGroups.Sorted := True;
        vGroups.Duplicates := dupIgnore;

        for i := 0 to AMessageErrorList.Count - 1 do
        begin
          vGroups.Add(AMessageErrorList[i].Source);
        end;

        for i := 0 to vGroups.Count - 1 do
        begin
          vListGroup := Frm_Erros.lvErrors.Groups.Add;
          vListGroup.Header := Format('%s (%d)', [vGroups[i], AMessageErrorList.GetCountForSource(vGroups[i])]);
          vListGroup.State := vListGroup.State + [lgsCollapsible];
          vListGroup.GroupID := i;
        end;

        for i := 0 to AMessageErrorList.Count - 1 do
        begin
          vMessage := AMessageErrorList.Items[i];
          Frm_Erros.lvErrors.Items.Add.Caption := vMessage.GetFullMessage;
          Frm_Erros.lvErrors.Items.Add.GroupID := vGroups.IndexOf(vMessage.Source);
        end;
      finally
        Frm_Erros.lvErrors.Items.EndUpdate;
        vGroups.Free;
        Frm_Erros.ShowModal;
      end;
      Frm_Erros.Free;
    end;

Solution

  • Your code is adding two items, assigning Caption to the first, GroupID to the second.

      Frm_Erros.lvErrors.Items.Add.Caption := vMessage.GetFullMessage;
      Frm_Erros.lvErrors.Items.Add.GroupID := vGroups.IndexOf(vMessage.Source);
    

    What you want to do is following I guess: Declare vListItem of appropriate type and then:

    vListItem := Frm_Erros.lvErrors.Items.Add;
    vListItem.Caption := vMessage.GetFullMessage;
    vListItem.GroupID := vGroups.IndexOf(vMessage.Source);
    

    Update:

    It might be possible that you add Column(s) at design time already, if not you need to

    vColumn := Frm_Erros.lvErrors.Columns.Add;
    vColumn.Caption := 'Error Message';
    vColumn.Width := 300; 
    

    to add them at run-time. Without columns I had the same blank Lines beneath the groups in a sample project.