0

I keep getting an error while attempting to add a range of items into my list view.

Error: "Cannot add or insert the item in more than one place"

Do keep in mind I have column headers.

Here is the example code:

private void OtherFunction()
    {
        // pulls info and had foreach loop
        Add_To_List(Event_Date, Acc_Name, Client_IP, Event_DC, Failure_Code);
    }

    private void Add_To_List(string date, string user, string ip, string domain, string lockedout)
    {
        listView1.ListViewItemSorter = null;

        // Add item to list view.
        ListView.ListViewItemCollection new_row = new ListView.ListViewItemCollection(listView1);
        new_row.Add(date);
        new_row.Add(user);
        new_row.Add(ip);
        new_row.Add(domain);
        new_row.Add(lockedout);
        listView1.Items.AddRange(new_row);

        // Clear data
        new_row.Clear();
    }

This was the code I was using before which worked but it wasn't quite how I wanted it.

string[] new_row = { user, ip, domain, lockedout };
listView1.Items.Add(date).SubItems.AddRange(new_row);

I was looking for an upgrade in speed.

Babyhamsta
  • 43
  • 5

1 Answers1

0

I ended up using this:

string[] row = { Event_Date, Acc_Name, Client_IP, Event_DC, Failure_Code };

var NewListViewItem = new ListViewItem(row);
listView1.Items.Add(NewListViewItem);
Babyhamsta
  • 43
  • 5
  • Hi Babyhamsta, glad to know you've found the solution to resolve this issue! Please consider accepting it as the answer to change its status to Answered. See [can I answer my own question..](https://stackoverflow.com/help/self-answer), Just a reminder :) – Jiale Xue - MSFT Dec 16 '21 at 08:13