1

我的应用程序读取一个文本文件并填充一个 ListView。这很简单,就像这样:

Date     | Invoice   | Status  
20121015 | 123123    |  
20121015 | 123124    | 
20121015 | 123456    |  
20121015 | 124123    |  

然后我需要阅读第二个文本文件,该文件可能包含也可能不包含在 ListView 中找到的发票以及状态。如果有匹配的发票,则需要将第二个文本文件中的状态添加到 ListView 中,因此它看起来像这样:

Date     | Invoice   | Status  
20121015 | 123123    |  
20121015 | 123124    | 
20121015 | 123456    | Paid 
20121015 | 124123    |  

最初我有一个只有发票号码的 ListBox,并且正在做

int index = ListBox1.FindString(<whatever>);

获取包含发票的行的索引,然后删除项目 (RemoveAt(Index)) 并插入一个新项目,如

ListBox.Items.Insert(index, invoice + " PAID")

如何使用 ListView 做类似的事情?我喜欢有列而不是只有 1 行文本的想法。我应该使用 ListView 以外的东西来完成这个吗?

平均而言,我正在阅读的每个文本文件都有 <1000 行需要添加。

4

1 回答 1

5

您可以枚举Items列表视图的集合。是的,listview 是对此的理想控制。

foreach (ListViewItem item in listView1.Items)
{
    var invoice = item.SubItems[1];
    if (invoice.Text == "whatever")
    {
        item.SubItems[2] = new ListViewItem.ListViewSubItem() { Text = "Paid" };
        break;
    }
}
于 2012-11-01T18:23:10.577 回答