Question

I don't know how can I update values. If I just update value like that:

objectListView1.Items[i].SubItems[1].Text = views;

after I hover cursor on item it back to old value. I tried use CellEditStarting event but with no effect (probably I used it wrong).

I tried update like that:

objectListView1.Invoke(new Action(() =>
{
     objectListView1.Items[i].SubItems[1].Text = views;
     objectListView1.Items[i].SubItems[2].Text = likes;
     objectListView1.Items[i].SubItems[3].Text = dislikes;
     objectListView1.Items[i].SubItems[5].Text = comments;
}));

objectListView1.Invoke(new Action(() => objectListView1.RefreshObject(objectListView1.Items[i])));

and that:

private void objectListView1_CellEditStarting(object sender, CellEditEventArgs e)
{
     e.Cancel = true;
     objectListView1.RefreshObject(e.RowObject);
}
Was it helpful?

Solution

Using the ObjectListView, you should never work directly with the underlying ListViewItem's. Those are managed internally. You only need to care about proper configuration of the OLV and the respective model objects.

Update and refresh the model items, NOT the ListViewItems!

You need to understand how a OLV is different from the classic ListView in terms of using it. The OLV homepage that you already referenced in your comment does a great job explaining the differences.

Resist the temptation to add, edit, remove, or otherwise mess with ListViewItems – it will not work.

...

Beware of ListViewItems. You never need to add ListViewItems to an ObjectListView. If you find yourself adding things to the Items collection, creating ListViewItems, or adding sub-items to anything, then you need to stop

Read and understand this, this and this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top