Search code examples
c#winformscomboboxselectedvalue

Convert SelectValue to int


I have a project in WinForm where I have a ComboBox. What I want to do is copy the value of the SelectedValue property into a variable of type int.

private void cbIzq_SelectedIndexChanged(object sender, EventArgs e)
{
    lblNombreIzq.Text = cbIzq.Text;
    int personaId = Int32.Parse(cbIzq.SelectedValue);
    lblEstaturaIzq.Text = GetEstatura(personaId) + " cm";
}

private string GetEstatura(int persona)
{
    return ListaMedidas.Where(x => x.persona == persona).Select(e => e.estatura).FirstOrDefault().ToString();
}

But it throws me the error:

cannot convert from 'object' to 'string'

What should I do to solve it?


Solution

  • See how to use Int32.Parse

    Int32.Parse Method

    Fixing it with the code below seems to solve the problem. int personaId = Int32.Parse(cbIzq.SelectedValue.ToString())