How a Checkbox can receive a Datagridview Checkboxcell

Asked

Viewed 144 times

0

In my Datagridview brings from the Database the user information, among them if he is administrator or not, this cell has a Datagridviewcheckboxcell.

inserir a descrição da imagem aqui

I want to pass its value (True/False) to a Checkbox Control. I tried using the following code, but it doesn’t work.

Debugging, I realized that the variable Adm is always True, even if it comes False from the Bank and the Checkbox even receiving the value, it is not marked, nothing happens.

Checkbox is by default with Checkstate Indeterminate

 private void tsbEditarUsuario_Click(object sender, EventArgs e)
        {
            if (gridConsultaUsuario.SelectedRows.Count > 0)
            {
                ucEditarUsuario editarUsuario = new ucEditarUsuario();
                editarUsuario.txtNome_detalhe.Text = gridConsultaUsuario.CurrentRow.Cells[0].Value.ToString();
                editarUsuario.txtUserCadastro_detalhe.Text = gridConsultaUsuario.CurrentRow.Cells[1].Value.ToString();
                editarUsuario.txtEmail_detalhe.Text = gridConsultaUsuario.CurrentRow.Cells[2].Value.ToString();
                editarUsuario.cboSetor_detalhe.SelectedItem = gridConsultaUsuario.CurrentRow.Cells[3].Value.ToString();
                editarUsuario.cboCargo_detalhe.SelectedItem = gridConsultaUsuario.CurrentRow.Cells[4].Value.ToString();

                var adm = (DataGridViewCheckBoxCell)this.gridConsultaUsuario.CurrentRow.Cells[5];

                editarUsuario.chkAdm_consulta.Checked = adm.Selected;
                _tabSystem.subTab(telaPrincipal.tabPrincipal.SelectedIndex, editarUsuario);
            }
            else
            {
                MessageBox.Show("Selecione um usuário");
            }
        }
  • Gabriel, for the record, you don’t need to tag visual-studio when the problem is not explicitly with the IDE. If interested, read What is a programming language, IDE and compiler?

  • Only with this piece of code it is difficult to know where the error is.

  • But what else is needed? I can post the entire method but I don’t know if it makes a difference.

2 answers

1

Try:

int index = gridConsultaUsuario.SelectedRows[0].Index;
ckAdm.Checked= Convert.ToBoolean(gridConsultaUsuario.Rows[index].Cells[5].Value);

0


After researching a little, I managed to solve the problem.

editarUsuario.chkAdm_consulta.Checked = Convert.ToBoolean(gridConsultaUsuario.CurrentRow.Cells[5].Value.ToString().ToLower());

Browser other questions tagged

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