How to work with DataGridViewCheckBoxColumn in C #?

0

I'm having some difficulties working with the data grid view selection field, I tried to do this:

public void Atualizar()
 {
     Stock obj = new Stock();

     for (int A = 0; A < dgv_Entrada.RowCount; A++)
     {

         int ID_Cod = Convert.ToInt32(dgv_Entrada[1, A].Value);

         obj.id_entrada = ID_Cod;
         obj.estado = cb_Estado.Text;
        }

       obj.Actualizar();

  }

When I execute, when I select more than one line, it only updates the data of the last line selected.

I'm not able to check the selected rows.

    
asked by anonymous 19.09.2017 / 12:03

1 answer

2

The code is wrong.

You are iterating through all the lines of your datagridview and after you have passed all and reached the last you are called the obj.Actualizar method; that is, it will only update the last one because it is saved in the variable.

The correct method is to call this method for each line, that is, put inside the loop for :

public void Atualizar()
{
   Stock obj = new Stock();

   for (int A = 0; A < dgv_Entrada.RowCount; A++)
   {
      // Checar se o valor é falso, se for, pula para a próxima linha.
      if(Convert.ToBoolean(dgv_Entrada[/*Nome ou indice coluna checkbox*/, A].Value) == false)
      {
         continue;
      }

      int ID_Cod = Convert.ToInt32(dgv_Entrada[1, A].Value);

      obj.id_entrada = ID_Cod;
      obj.estado = cb_Estado.Text;

      obj.Actualizar();
   }
}
    
19.09.2017 / 14:14