DataGridView C #

0

I have a DataGridView in this way:

and this is my code

Clicking on the register button does this:

 foreach (DataGridViewRow item in dgvEstudiantes.Rows)
        {

            int indice = dgvEstudiantes.CurrentRow.Index;
            MessageBox.Show(""+indice);
            DataGridViewCheckBoxCell objCheckBox = (DataGridViewCheckBoxCell)item.Cells[4];
            if (objCheckBox.Value == objCheckBox.TrueValue)
            {

                objAsistencia.IdCurso = int.Parse(cmbAsistenciaCurso.SelectedValue.ToString());
                objAsistencia.Estado = 1;
                objAsistencia.HorasVistas = int.Parse(txtHorasvistas.Text);
                objAsistencia.Fecha = dtpFechaAsistencia.Value.Year + "-" + dtpFechaAsistencia.Value.Month + "-" + dtpFechaAsistencia.Value.Day;
                objAsistencia.registrarAsistencia();
                MessageBox.Show("Sirve boton");
            }
            else {
                MessageBox.Show("no sirve boton");
            }
        }

But the problem that I present is that the index always shows this

therefore the record of document 122222 never does. Could you help me start at 122222?

    
asked by Lina Cortés 22.06.2016 в 03:24
source

4 answers

2

You have to use the variable item which is the one that iterates through each row to know the index

why use

MessageBox.Show(item.Index);

the code would be

foreach (DataGridViewRow item in dgvEstudiantes.Rows)
{

    MessageBox.Show(item.Index);
    DataGridViewCheckBoxCell objCheckBox = (DataGridViewCheckBoxCell)item.Cells[4];
    if (Convert.ToBoolean(objCheckBox.Value))
    {

        objAsistencia.IdCurso = int.Parse(cmbAsistenciaCurso.SelectedValue.ToString());
        objAsistencia.Estado = 1;
        objAsistencia.HorasVistas = int.Parse(txtHorasvistas.Text);
        objAsistencia.Fecha = dtpFechaAsistencia.Value.Year + "-" + dtpFechaAsistencia.Value.Month + "-" + dtpFechaAsistencia.Value.Day;
        objAsistencia.registrarAsistencia();
        MessageBox.Show("Sirve boton");
    }
    else {
        MessageBox.Show("no sirve boton");
    }
}
    
answered by 22.06.2016 в 06:36
1

You could simply go through your datagridview and get the rows that are with the check and then know their positions or the values that each cell has:

foreach (DataGridViewRow row in dgvEstudiantes.Rows)
{
    if(row.Cells[4].Value == true)
    {
        MessageBox.Show(row.Index.ToString());
    }

}
    
answered by 22.05.2017 в 06:22
0

The index you indicate does not influence the error behavior of the code. It shows you "2" because that is the selected record.

The problem I think is here:

DataGridViewCheckBoxCell objCheckBox = (DataGridViewCheckBoxCell)item.Cells[4];
if (objCheckBox.Value == objCheckBox.TrueValue)

Test:

CheckBox objCheckBox = (CheckBox) item.Cells[4].FindControl("NombreDeTuCheckBoxAsistencia");
if (objCheckBox.Checked)

The index I see it as irrelevant, unless it is used elsewhere and still it will show you the selected record nothing else.

    
answered by 22.06.2016 в 05:45
0

Item.Index is used because if we use dgvEstudiantes.CurrentRow.Index it gives the index that is currently selected in the application.

    
answered by 22.06.2016 в 15:35