Answered by:
How do I replace an item in a generic list

Question
-
Don't know why I am blanking on this ... but isn't there a way to replace an item in a generic list?
I have a List<Discount>. If the user changes some of the key data in the system, the discount data needs to change accordingly. So I want to just create a new discount and replace the old one. Do I have to actually remove and add? Or is there another way to replace an item in a generic list?
Thanks!
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!Wednesday, October 20, 2010 6:58 PM
Answers
-
What type of object is Discount? (struct, class, etc)
The data grid should contain a reference to the underlying Discount object, not a copy of the object. That is, unless Discount is actually a struct and not a class? If that is the case, I would ask why does it need to be a struct?
That, or you are explicitly making a copy of the Discount object somewhere between the List and the DataGridView?
Alternatively, you could potentially rely on the row's index. If the rows are presented to the user in the same order as they exist in your list (and not sortable), then the index is just e.Row.Index I think.
- Marked as answer by DeborahKMVP Wednesday, October 20, 2010 7:37 PM
Wednesday, October 20, 2010 7:31 PM
All replies
-
The indexer will replace an existing item in a list, just like it would for an array. Ex:
List<Discount> discounts;
...
discounts[someIndex] = new Discount(...);
http://msdn.microsoft.com/en-us/library/0ebtbkkc.aspx
- Proposed as answer by Reed Copsey, JrMVP Wednesday, October 20, 2010 7:06 PM
Wednesday, October 20, 2010 7:04 PM -
OK, how do I get the index?
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!Wednesday, October 20, 2010 7:12 PM -
It happens, above method will work, if you need to pinpoint what you want to replace, you can use any of the available methods in List or a loop.
Remeber that list only stores the references so above operation will move discounts[someIndex] reference to a new object of your choosing, it will not delete the object that was referenced before.
Regards
Edit: http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx
Indexof will get you the index
Wednesday, October 20, 2010 7:15 PM -
I have the items bound to a DataGridView. I am using the DataGridView's DataBoundItem property:
Discount myDiscount = (Discount)e.Row.DataBoundItem;
But if I change any of the properties of myDiscount, it is not changing in the list. So I assume that the gird row is not giving me the reference. Hence the reason I need to replace the value in the list.
I don't yet have a unique identifier on the item because this is all in the UI prior to any saving and assigning of unique key values. So I can't really use Linq to retrieve the matching element from the list.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!Wednesday, October 20, 2010 7:26 PM -
What type of object is Discount? (struct, class, etc)
The data grid should contain a reference to the underlying Discount object, not a copy of the object. That is, unless Discount is actually a struct and not a class? If that is the case, I would ask why does it need to be a struct?
That, or you are explicitly making a copy of the Discount object somewhere between the List and the DataGridView?
Alternatively, you could potentially rely on the row's index. If the rows are presented to the user in the same order as they exist in your list (and not sortable), then the index is just e.Row.Index I think.
- Marked as answer by DeborahKMVP Wednesday, October 20, 2010 7:37 PM
Wednesday, October 20, 2010 7:31 PM -
Oh, databound, that can be messy sometimes.
The safer bet would be to use databinding so that the heavy lifting of synchronizing the control with the collection is done by C#.
As you are using a DGV it will be a bit more messier than if you where using wpf.
Regards
Wednesday, October 20, 2010 7:35 PM -
Doh! I am updating someone elses code and they did indeed change the instance between the time the code was retrieving it using the DataBoundItem and the time I was updating it. Hence the reason my changes were never shown in the grid.
Thanks for making me look at it again. (And yes it is a class)
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!Wednesday, October 20, 2010 7:39 PM -
Glad it's working - might be a good idea to look over it and make sure that some other code isn't RELYING on it being a copy for some reason - ex: does that list happen to be named "originalDiscounts"? :)Wednesday, October 20, 2010 7:41 PM
-
LOL. Good idea.
I know we have gone back and forth and back again on how to handle Cancel. I would assume it was left over from one of those trials. I'll check.
Thanks again!
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!Wednesday, October 20, 2010 7:54 PM