Search code examples
c#comboboxselectedindex

InvalidArgument=Value of '5' is not valid for 'SelectedIndex'


I read saved data from tbl in a list, and i want to edit the object, so when i start the program, combobox first to show saved value for that object, and others also to be in the combobox. Please help !

if (lstP.Count > 0)
{
    for (int i = 0; i < lstP.Count; i++)
    {
        if (Stav.IDP == lstP[i].SP)
        {
            Prim.SelectedIndex = lstP[i].SP;
            //ERROR
            break;
        }
    }
}

Solution

  • SelectedIndex requires a number to be passed. What you need is to assign an i to it:

    if (lstP.Count > 0)
    {
        for (int i = 0; i < lstP.Count; i++)
        {
            if (Stav.IDP == lstP[i].SP)
            {
                Prim.SelectedIndex = i;
                break;
            }
        }
    }