Create Id in a DataGridView

0

Hi, I have a DGV in which, through a button, I want to create its correlative code when I press the button

private void btnNuevoClasificacion_Click(object sender, EventArgs e)
    {
        ClasificacionTalla ct = new ClasificacionTalla();

        int Id = (from item in listCT select (item.ClasificacionTallaId)).Max();
        int resultado = Id + 1;


        listCT.Add(ct);
        dgvClasificacion.DataSource = null;
        dgvClasificacion.DataSource = listCT;
    }

I get an error in the line of the id Id the sequence does not contain elements

    
asked by Pedro Ávila 30.04.2016 в 18:55
source

1 answer

1

But there are items in the list you should validate before applying the Max ()

private void btnNuevoClasificacion_Click(object sender, EventArgs e)
{
    int resultado = 1;

    if(listCT.Count > 0){
        int Id = (from item in listCT select (item.ClasificacionTallaId)).Max();
        resultado = Id + 1;
    }

    ClasificacionTalla ct = new ClasificacionTalla();
    ct.ClasificacionTallaId = resultado;
    listCT.Add(ct);

    dgvClasificacion.DataSource = null;
    dgvClasificacion.DataSource = listCT;
}
    
answered by 01.05.2016 / 00:18
source