How would I go about about replacing the text of blank items in column 5 of my listView with the word “No”?
I tried this but it threw an InvalidArgument=Value of '4' is not valid for 'index'. error:
foreach (ListViewItem i in listView1.Items)
{
if (i.SubItems[4].Text == " ")
{
i.SubItems[4].Text = i.SubItems[4].Text.Replace(" ", "No");
}
}
The code provided above will get all items within
ListView1.Itemsand check if the sub-item of index4and its propertyTextis equal towhich may result in the described error if the index exceeds the array limit. You may avoid this by making sure that this item is notNothing.Example
Notice: Arrays are zero-indexed which means that they start with 0 instead of 1.
Notice: If you only have 4 columns,
i.SubItems.Countwould be 4 and not 3 because it’s a normalintconsidering that all columns are filled.Thanks,
I hope you find this helpful 🙂