上面提供的代码将获取其中的所有项目ListView1.Items
并检查索引的子项目4
及其属性Text
是否相等,
如果索引超出数组限制,则可能导致描述的错误。您可以通过确保该项目不是 来避免这种情况Nothing
。
例子
foreach (ListViewItem i in listView1.Items) //Get all items in listView1.Items
{
if (i.SubItems.Count > 3) //Continue if i.SubItems.Count is more than 3 (The array contains i.SubItems[3] which refers to an item within the 4th column (i.SubItems.Count is not an array. Therefore, it'll start with 1 instead of 0))
{
if (i.SubItems[3].Text == " ") //Continue if i.SubItems[3].Text is equal to
{
i.SubItems[3].Text = i.SubItems[3].Text.Replace(" ", "No"); //Replace with No
}
}
}
注意:数组是零索引的,这意味着它们以 0 而不是 1 开头。
注意:如果您只有 4 列,i.SubItems.Count
则为 4 而不是 3,因为int
考虑到所有列都已填充,这是正常现象。
谢谢,
我希望你觉得这有帮助:)