c# - 如何在 ListView 中查找项目并更新同一行中的另一列?

标签 c# winforms listview

我的应用程序读取一个文本文件并填充一个 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    |  

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

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

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

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

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

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

最佳答案

您可以枚举 ListView 的 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;
    }
}

关于c# - 如何在 ListView 中查找项目并更新同一行中的另一列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13183027/

相关文章:

c# - 尽管提供的服务器路径正确,打印服务器仍抛出 "Win32 error: The printer name is invalid."异常

android - 如何删除使用 addTextChangedListener 添加的所有监听器

c#:在子项上显示工具提示

java - 访问 ListView 项目字符串的最简单方法是什么?

c# - 在 C# 中选择名称相同但属性内部文本不同的 XML 节点

c# - SQL语句获取一周前的数据?

c# - 将xml内容转换为C#中的键值对

c# - 设置并发作业的最大值 Quartz.Net

c# - Winforms - MVP 模式 : Using static ApplicationController to coordinate application?

c# - 由于 System.Windows.Point/System.Drawing.PointF 差异,在 WPF/Silverlight 和 GDI+ 之间共享库时出现问题