Reputation: 1680
I am trying to figure out how to update a Object's property in a ListView after render. For an example, let's say that the ListView is DataBound to a collection of Employees. Each row displays the information of the employee. After the table is loaded, I needed to say "If an employee name = [RON] then change it to text [RONALD]".
I currently was thinking I could foreach the ListViewDataItems in the ListView, and go from there, but am stuck. Any help would be appreciated.
foreach(ListViewDataItem entry in lvProjectModeratorEntries.Items)
{
//I need to find the div where the firsName is
//displayed, and run my logic to update it.
}
I also thought I would get it through entry.DataItem but am stuck at that point.
Upvotes: 1
Views: 511
Reputation: 432
Using your own code:
foreach (ListViewItem item in lvTest.Items)
{
if (item.Text == "John")
item.Text = "John is gone";
}
That should give you a start on how to do things, yet I don't recommend it. There are more elegant and code-sustainable solutions. Have you thought about binding the listview to a List and make all the necessary business logic in the List, instead of the actual view?
Upvotes: 0
Reputation: 4638
to find each item in a Listview you can loop like this
for (int i = 0; i < lvProjectModeratorEntries.Items.Count; i++)
{
int ii = 1;
MessageBox.Show(lvProjectModeratorEntries.Items[i].SubItems[ii].Text);
ii++;
}
Upvotes: 0
Reputation: 164
It sounds like you could use the INotifyPropertyChanged Interface on your Employee model. In your listview item template, bind the text to the Employee's property that you wish to display. Then you can change your Employee objects.
Upvotes: 1