How to Update ListViewItems in a Listview

Here are 2 ways to update values in a ListView with C#:

private void Form1_Load(object sender, EventArgs e)
{
 // Add some items.
 ListViewItem lvi = new ListViewItem();
 lvi.Text = "First";
 lvi.SubItems.Add("2nd");
 listView1.Items.Add(lvi);
 lvi = new ListViewItem();
 lvi.Text = "Third";
 lvi.SubItems.Add("4th");
 listView1.Items.Add(lvi);
}

private int i = 0;
private int j = 0;

private void button1_Click(object sender, EventArgs e)
{
 // update by item index
 listView1.Items[0].Text = "Updated (" + j.ToString() + ")";
 listView1.Items[0].SubItems[1].Text = "and here (" + j.ToString() + ")";
 j++;
}

private void button2_Click(object sender, EventArgs e)
{
 // update by searching for text in subitems.
 // find a reference for the listviewitem with specific text in any of it's subitems.
 ListViewItem lvi = listView1.FindItemWithText("4th");
 if (lvi != null)
 {
 // Using the reference, update the value.
 lvi.SubItems[0].Text = "Updated by reference (" + i.ToString() + ")";
 i++;
 }
}

You can download the project here: ListViewExample

Update ListView values

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

The store is under construction. Check back in a few days. Dismiss

Exit mobile version