I have some code shown below that writes the values of a gridview to a list as shown.
The code does get the correct values but when the second row of the grid is added to the list it overwrites the first row of the list.
Anybody know why this is happening?
C# Code
List<Item> lstNewItems = new List<Item>(); // Control Items
lstNewItems.Clear();
Item NewItem = new Item();
foreach (GridViewRow PendingItemUnderControl in GridViewPendingList.Rows)
{
NewItem.Paramater = PendingItemUnderControl.Cells[0].Text.ToLower();
NewItem.Type = (String)Session["BrowseType"];
lstNewItems.Add(NewItem);
}
You are creating one instance of
Itemclass. It happens before your loop, so in reality you are working on the same object. You need to create new instance inside loop (in each iteration):