I have a listView1 in C# WinForms which displays 2 List
List<string> pths;
List<string> rec;
public void Disp ()
{
DisplayListInColumns(listView1, pths, 0);
DisplayListInColumns(listView1, rec, 1);
}
private static void DisplayListInColumns(ListView listView, List<string> values, int columnIndex)
{
for (int index = 0; index < values.Count; index++)
{
while (index >= listView.Items.Count)
{
listView.Items.Add(new ListViewItem());
}
ListViewItem listViewItem = listView.Items[index];
while (listViewItem.SubItems.Count <= columnIndex)
{
listViewItem.SubItems.Add(new ListViewItem.ListViewSubItem());
}
listViewItem.SubItems[columnIndex].Text = values[index];
}
}
I am using the global List to make the changes and also display it in the listview1 but only after the user clicks apply_button will the changes be saved (on to a xml).
The Edit and Add details button is working fine and displaying perfectly. But when I delete the data, I am thrown an error.
The following is the delete action:
//Configuration - DELETE button
private void button6_Click(object sender, EventArgs e)
{
string select = null;
if (listView1.SelectedItems.Count > 0)
{
select = (listView1.SelectedItems[0].Text);
}
int count = listView1.SelectedItems[0].Index;
if (select != null)
{
pths.RemoveAt(count);
rec.RemoveAt(count);
string s = String.Join("; ", pths.ToArray());
string r = String.Join("; ", rec.ToArray());
//MessageBox.Show(s);
//MessageBox.Show(r);
}
Disp();
}
I think after few tries, I am going wrong with the index. Even after deleting, while debugging I get the listView.Items.Count = 5.
I am guessing the count is still 5 (sample - 5 string within the list) when after deleting it should reduce to 4 and the index 0-3 accordingly.
I am getting the following eror
ArgumentOutOfRangeException at pths.RemoveAt(count)
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
As an alternate I tried pths.Remove(select);
but did not resolve this.
Any help would be appreciated. Thank you
Sounds like you just need to reload your list...
//Clear it
listView1.Items.Clear();
//reload it.
listView1.Refresh();