Issue with List.Add() only saving the last added item [duplicate]

You need to instantiate the Orderables within the for loop; otherwise, you keep reusing the same instance in all iterations (and overwriting its properties each time).

AssociatedComboItems ai = new AssociatedComboItems();
List<Orderables> tempList = new List<Orderables>();

foreach (var t in comboBox1.Items)
{
    ai.ComboBoxItem = t.ToString();

    for (int i = 0; i < fpSpread1.ActiveSheet.RowCount; i++)
    {
        Orderables orderables = new Orderables();  // ← Instantiate here
        orderables.Display = fpSpread1.ActiveSheet.Cells[i, 1].Text;
        orderables.ShowInDSR = (bool)fpSpread1.ActiveSheet.Cells[i, 0].Value;
        orderables.DisplayOrder = i;
        tempList.Add(orderables);
    }

    ai.AssociatedItems = tempList;
    tempList.Clear();
    if(AssociatedItems == null)
    AssociatedItems = new List<AssociatedComboItems>();
    AssociatedItems.Add(ai);
}

Unrelated to the question: You might find object initializer syntax to be cleaner:

Orderables orderables = new Orderables
{
    Display = fpSpread1.ActiveSheet.Cells[i, 1].Text,
    ShowInDSR = (bool)fpSpread1.ActiveSheet.Cells[i, 0].Value,
    DisplayOrder = i,
};

Leave a Comment