Link to home
Start Free TrialLog in
Avatar of larockd
larockd

asked on

ListView - Update A Single Item

I was wondering what is the best approach to updating single items in a listview.  I could clear all the items in the listview and then read them with the updated information, but that doesnt sound like the best approach when all I need to do is update a single cell for a
single item especially if the listview has 50+ items in it.

Any ideas?

Thanks
dl
ASKER CERTIFIED SOLUTION
Avatar of CJ_S
CJ_S
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of larockd
larockd

ASKER

Thank you that was exactly what I was looking for.

dl
You should also consider using the Tag property of WinForms controls. When populating the listview, each listitem has a Tag property. This allows you to associate the relevant object for that row.

So if you're populating a listview with a collection of people, you could use:

ListViewItem lvi = null;

foreach (Person p in persons)
{
    lvi = new ListViewItem();

    lvi.Tag = p;
    lvi.Text = p.FirstName;

    this.ListView.Items.Add(lvi);
}

Open in new window


So the next time you need to update a particular row, you can use the Person object to find it instead of remembering indexes. This makes it far more natural to access items programatically.