【发布时间】:2012-01-11 15:56:30
【问题描述】:
我正在尝试清除并重新添加 .net 中 ListViewItem 的 SubItems。
private void RefreshItem(ListViewItem item)
{
item.Text = accountNumber;
item.SubItems.Clear(); //in case there are any
item.SubItems.Add(name);
item.SubItems.Add(address);
item.SubItems.Add(phone);
item.SubItems.Add(workphone);
item.SubItems.Add(email);
item.SubItems.Add(idType);
item.SubItems.Add(idNumber);
item.SubItems.Add(idExpires);
...
}
但清除子项也会清除Text。
注意:矛盾的是,清除
SubItems也会清除Text,但添加SubItems不会同时添加Text。
当我只想更新子项时也会出现问题:
private void RefreshItem(ListViewItem item)
{
item.SubItems.Clear(); //in case there are any
item.SubItems.Add(location);
item.SubItems.Add(date);
item.SubItems.Add(cashier);
item.SubItems.Add(totalBuyAmount);
item.SubItems.Add(totalSellAmount);
item.SubItems.Add(currencyCode);
item.SubItems.Add(exchangeRate);
item.SubItems.Add(isVip);
...
}
我怎么能ClearSubItemsListViewItem,却不清除Text?
【问题讨论】:
-
能不能不把
Text存入局部变量,把SubItems清空再重新设置Text?也许不是最理想的解决方案。 -
我不明白您为什么要清除子项而不是与它们关联的文本。
-
@John 我想你可能误解了。
Text是ListItem本身的文本。 ListItem 然后可以有子项。我想清除 ListItem.SubItems 集合,但保留 ListItem.Text 属性。 -
@Ian Boyd:试试 adrift 说的话。
标签: c# winforms listview listviewitem