0

This is the error I'm getting. Basically what is happening is I'm creating a new product, associating parts to the new product but after the new product has been created and added to my data grid view (save button), when I return to modify the product and add additional parts, I get this error 2

This is my modify product page. Everything works until this point. This is a newly created product (I have pre-populated the main form grids) but when I click to add a new part, I get that error message from the screenshot. 1

This is my button for adding parts (bindinglist) to associated parts grid for the newly created product.

        private void BtnAdd_Click(object sender, EventArgs e)
    {
        Part part = (Part)dgvAllParts.CurrentRow.DataBoundItem;
        prodToMod.AssociatedParts.Insert(part.PartID - 1, part);
    }
Benn_ht
  • 5
  • 2
  • what is the data type of AssociatedParts, is it a `List` ? You need to give info about the classes and data types, rather than screenshot of your application. The `exception` clearly states you are accessing `out of bounds` - if the PartID-1 becomes negative you will get this. – Anand Sowmithiran May 21 '22 at 15:27
  • public BindingList AssociatedParts = new BindingList(); The screenshot was a visual reference to what I was trying to put into words. – Benn_ht May 21 '22 at 15:35
  • The only reason for the exception you got is that when your code calls `Insert` function that first argument evaluates to less than zero. check by setting breakpoint and inspect the value of `part.PartID`, and fix it. – Anand Sowmithiran May 21 '22 at 15:40
  • Also, check if the `AllowNew` property of `BindingList` is set to **true**, refer [this](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.bindinglist-1.allownew?view=net-6.0) – Anand Sowmithiran May 21 '22 at 15:41

0 Answers0