How to work with Datagridviewcheckboxcolumn in c#?

Asked

Viewed 28 times

0

I’m having some difficulty working with the data grid view selection field, I tried to do it this way:

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 running, when selecting but from a row, it only updates the data of the last selected row.

I’m not getting a check of the selected lines.

1 answer

2


The code is wrong.

You’re iterating through all the lines of your datagridview and after you’ve gone through all and arrived at the last you’re called the method obj.Actualizar; that is, it will only update the last because it is she who was saved in the variable.

The correct thing is for you to call this method to each line, ie 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();
   }
}
  • How do I do that? You can give a practical example?

  • Already made, look at the code I posted, I put the method obj.Actualizar within the for, that’s all

  • By doing so he saves all the lines, even the ones I didn’t select, you see made this way. Tentei fazer uma verificação das linhas que estão a ser selecionado desta forma: int ID_Cod = Convert.ToInt32(dgv_Entrada[1, A].Value);&#xA; Boolean x = Convert.ToBoolean(dgv_Entrada["chk_opcao", dgv_Entrada.CurrentRow.Index].Value.ToString()); if(x==true) { obj.id_input = Id_cod; obj.status = cb_Estate.Text; } Else { } but did not result

  • Ready, I put a check to see if the value is selected or not. Take a look, just put the name or Dice of the checkbox column.

  • Now it’s working, thanks for your help. If I want to add an option to select everything with a checkbox apart, will it work if I put the name of datagridview . Selectall ?

  • You’re welcome. You can only select everything programmatically. Follow this answer https://stackoverflow.com/questions/13242861/check-all-checkbox-items-on-datagridview

  • I got what I wanted, it’s already working, but thank you for everything!

  • Just mark my answer as accepted, please

  • I found but another difficulty, I was able to add a checkbox apart with the option to select everything and it works normally, it happens that when I select for example two lunhas and click on select everything, it disables the two previously selected fields and enables the other fields that were not selected, there is the code: foreach (Datagridviewrow Row in dgv_Entrada.Rows) { Datagridviewcheckbcell chk = (Datagridviewcheckboxcell)Row. Cells[0]; chk.Value = ! (chk.Value == null ? false : (bool)chk.Value); }

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.