Jeff Brady
Jeff Brady

Reputation: 1498

How do I find an item in a ListView and update another column in that same row?

My app reads a text file and populates a ListView. It's pretty simple, like this:

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

I then need to read a 2nd text file, which may or may not contain an invoice found in the ListView along with a status. If there is a matching invoice, the status from that 2nd text file needs to be added to the ListView so it would look like this:

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

Originally I had a ListBox with only the invoice numbers, and was doing

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

to get the index of the row containing an invoice, then removing the item (RemoveAt(Index)) and inserting a new item like

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

How do I do something similar with a ListView? I like the idea of having columns instead of just 1 line of text. Should I use something other than a ListView to accomplish this?

On average, each text file I'm reading has <1000 rows that need to be added.

Upvotes: 1

Views: 6771

Answers (1)

nawfal
nawfal

Reputation: 73173

You can enumerate through the Items collection of your listview. And yes listview is the ideal control for this.

foreach (ListViewItem item in listView1.Items)
{
    var invoice = item.SubItems[1];
    if (invoice.Text == "whatever")
    {
        item.SubItems[2] = new ListViewItem.ListViewSubItem() { Text = "Paid" };
        break;
    }
}

Upvotes: 5

Related Questions