I have a list view
that has two columns: one for the prefix and one for the connection string.
Example:
Prefix| Connection string
--------------------------
OR1_ | blablabla
--------------------------
OR2_ | blebleble
--------------------------
OR3_ | blublublu
--------------------------
(Sorry for the makeshift table, I think it will be easier to understand)
So now whenever I delete an item from the listView I need to rename all the prefixes so imagine if I delete the item number 2, I will end up with:
Prefix| Connection string
--------------------------
OR1_ | blablabla
--------------------------
OR3_ | blublublu
--------------------------
But I actually need it to be OR1_ and OR2_
so I made this code that does that when I delete an item:
private void buttonDel_Click(object sender, EventArgs e)
{
if (listViewConnectionStrings.Items.Count == 0) return;
else
{
//Delete the items
foreach (ListViewItem eachItem in listViewConnectionStrings.SelectedItems)
{
Connections.Remove(eachItem.Text);
listViewConnectionStrings.Items.Remove(eachItem);
}
if(listViewConnectionStrings.Items.Count == 0) return;
//Rename the prefixes
int cntItems = listViewConnectionStrings.Items.Count;
for (int i = 0; i <= cntItems; i++)
{
ListViewItem newItem = new ListViewItem();
newItem = listViewConnectionStrings.Items[0];
int newPrefix = i++;
newItem.Text = @"OR" + newPrefix.ToString() + @"_";
}
}
}
The last for
is working fine when I delete the first item on the list, it renames all the others properly, but If I delete one in the middle or at the bottom, well one that's not the first all the others get the same name.
What am I missing, what's wrong?