I've adopted the following pattern for putting ListViewItems in a ListView with multiple columns (when I want to display information about a list of MyObject
types), and I'm just curious to see if this is the best way to accomplish this task, or if there's anything more efficient and readable in code:
- Create an inherited
ListViewItem
class that takes aMyObject
object in the constructor - I'll call thisMyObjectListViewItem
- and aRefresh()
method that clears and re-populates the ListViewItem subitems. - Populate the ListView with my new MyObjectListViewItem items.
example:
public MyObject MyObject { get; set; }
public MyObjectListViewItem(MyObject myObj)
{
MyObject = myObj;
this.Refresh();
}
public void Refresh()
{
this.SubItems.Clear();
this.Text = myObj.FirstColumnProperty;
this.SubItems.Add(myObj.SecondColumnProperty); // etc...
}
Suggestions? Better ways?