Home > Software engineering >  Cannot add or insert the item in more than one place - ListView Error
Cannot add or insert the item in more than one place - ListView Error

Time:12-23

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.

CodePudding user response:

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);
  • Related