Sounds like you just need to reload your list...
//Clear it
listView1.Items.Clear();
//reload it.
listView1.Refresh();
Question
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
Solution 3
Sounds like you just need to reload your list...
//Clear it
listView1.Items.Clear();
//reload it.
listView1.Refresh();
OTHER TIPS
Change your if statement
if (select != null)
to this
if(!string.IsNullOrWhiteSpace(select))
Your text property won't be null, it will be empty string and so you are entering that section when you think you aren't.
EDIT:
Based on your comments, i'm going to direct you to this solution instead, replace your entire delete function with something like this:
private void button6_Click(object sender, EventArgs e)
{
foreach (ListViewItem eachItem in listView1.SelectedItems)
{
listView1.Items.Remove(eachItem);
if (pths.Any(o => o == eachItem.Text))
{
pths.Remove(eachItem.Text);
}
if (rec.Any(o => o == eachItem.Text))
{
rec.Remove(eachItem.Text);
}
}
}
You may need eachItem.Value instead, but I think .Text will work.
NOTE: This answer I absolutely just copied from here (I take no credit for this solution):
You need to verify your SelectedItem
first before trying to remove it from the List
.
private void button6_Click(object sender, EventArgs e)
{
string select = (listView1.SelectedItems.Count > 0) ? (listView1.SelectedItems[0].Text) : null;
if (!string.IsNullOrWhiteSpace(select))
{
listView1.BeginUpdate();
pths.Remove(select);
rec.Remove(select);
listView1.EndUpdate();
string s = String.Join("; ", pths.ToArray());
string r = String.Join("; ", rec.ToArray());
}
Disp();
}