OP:
, , - .
.
// Create dictionary.. Can be done somewhere else..
var dictionary = new Dictionary<int, string>();
dictionary.Add(1, "Item 1");
dictionary.Add(2, "Item 2");
// You can set up the column in the designer as well.
objectListView1.Columns.Add(new OLVColumn(title: "Items", aspect: "Value"));
// Initially tells OLV to use the dictionary as a datasource.
objectListView1.SetObjects(dictionary);
// .....
// Later on, you can add another item to the dictionary.
dictionary.Add(3, "Item 3");
// All you have to do now, is call .BuildList(), and your listview is updated.
// shouldPreserveState can be false if you want. I want it to be true. :)
objectListView1.BuildList(shouldPreserveState:true);
" ", , SetObjects() . , BuildList , .
@ElektroStudios
, - ListViewItem " ". OLV, @Grammarian, ListViewItem - , .
"", write a 1000 lines model class... Just to add 1 string to a ListView. . .
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var items = new List<ListViewItem>
{
new ListViewItem(new []{"Hello", "Stack","Overflow"}),
new ListViewItem(new []{"ObjectListView is pretty damn neat!"}),
new ListViewItem(new []{"Pretty", "Cool"})
};
olvColumn1.AspectGetter = SubItemGetter(0);
olvColumn2.AspectGetter = SubItemGetter(1);
olvColumn3.AspectGetter = SubItemGetter(2);
for (int index = 0; index < objectListView1.Columns.Count; index++)
{
OLVColumn column = objectListView1.AllColumns[index];
column.AspectGetter = SubItemGetter(index);
}
objectListView1.SetObjects(items);
items.Add(new ListViewItem(new []{"I","Dont","Care","How","Many","SubItems","There","Is!"}));
objectListView1.BuildList(shouldPreserveState:true);
}
private AspectGetterDelegate SubItemGetter(int subItemIndex)
{
return rowObject =>
{
var lvi = (ListViewItem) rowObject;
if (lvi.SubItems.Count <= subItemIndex)
return null;
return lvi.SubItems[subItemIndex].Text;
};
}
}
OLV , ( , !)..

source
share