Specified cast is not valid

0

I am developing an Academic Registration application in C #. I have some object composers like:

public class Turma
{
    public String Descricao { get; set; }
    public int QtdAlunos { get; set; }
    public Turno turno { get; set; }
    public Curso curso { get; set; }
    public Semestre semestre { get; set; }
    public Nivel nivel { get; set; }
    public int Ano { get; set; }
    public String Sala { get; set; }
}

The difficulty and the following, I have a procedure search in SqlServer that receives parameters of type int of objects: Turno , Curso , Semestre and Nivel . Populate the comboBox with their respective objects as follows:

public static void Nivel()
{
    NivelController nivelController = new NivelController();
    NivelColecao nivelColecao = nivelController.NivelConsultar();

    cboNivel.DataSource = null;
    cboNivel.DataSource = nivelColecao;

    cboNivel.DisplayMember = "Descricao";
    cboNivel.ValueMember = "IDNivel";

    cboNivel.Update();
    cboNivel.Refresh();
}

So .. what should happen is that in selectedIndex of ComboBox should update to DataGridView .

private void cboNivel_SelectedIndexChanged(object sender, EventArgs e)
{
        turma.nivel = new Nivel();
        turma.nivel.IDNivel = (int)cboNivel.SelectedValue;
}

The error is as follows:

  

SpecifiedCast is not valid and Unable to cast object of type   'Model.Level' to type 'System.IConvertible'.

publi  void Turma(Turma turma) 
{
    try
    {
        TurmaController turmaController = new TurmaController();
        TurmaColecao turmaColecao = turmaController.TurmaConsultar(turma);

        var TurmaListar = turmaColecao.Select(Turma => new
        {
            Turma = Turma.Descricao,
            QtdAlunos = Turma.QtdAlunos,
            Sala = Turma.Sala,
            Ano = Turma.Ano,
            Curso = Turma.curso.Descricao,
            Turno = Turma.turno.Descricao,
            Nivel = Turma.nivel.Descricao,
            Semestre = Turma.semestre.Descricao

        }).ToList();

        dGV.DataSource = null;
        dGV.DataSource = TurmaListar;

        dGV.AutoResizeColumns();

        dGV.Update();
        dGV.Refresh();
    }
    catch (Exception ex)
    {
        MessageBox.Show("Erro ao fazer a consulta. Detalhes: " + ex.Message);
    }
} 
    
asked by anonymous 09.11.2016 / 21:29

0 answers