Check Whether Datagridview field of type Datagridviewcheckboxcolumn is marked

Asked

Viewed 838 times

0

I have a dataGridView and do the check to see if in the column of type Datagridviewcheckboxcolumn there are any selected lines. If there is at least one line with this selected field executing an action type, otherwise it triggers a message of type Messabox.

Int32 verificaCheckBoxGrid = 0;

//varrendo o dataGridView
for (Int32 index = 0; index < meuDataGridView.Rows.Count; index++) {
  // verifica se pelo menos 1 checkbox do grid esta ativado
  if (bool.Parse(meuDataGridView.Rows[index].Cells[0].FormattedValue.ToString()) == true) {
    verificaCheckBoxGrid++;
  }
}

// se não conter pelo menos 1 checkbox ativado no dataGridView
if (verificaCheckBoxGrid < 1 || meuDataGridView.Rows.Count == 0) {
  MessageBox.Show("Nenhuma Nota Selecionada", "A T E N Ç Ã O", MessageBoxButtons.OK, MessageBoxIcon.Information);
  return;
}

I would like to do something that would be with the most presentable code as for example in this case below that is checked if any of the radiobutton’s that are in a groupBox has at least one selected, according to this question.

// usando LINQ -  verifica se todos os checkbox dentro do grupo box estão desmarcados
if (!gbTipoDePagamento.Controls.OfType<RadioButton>().Any(x => x.Checked)) {
  MessageBox.Show("Selecione Um Tipo De Pagamento", "A T E N Ç Ã O", MessageBoxButtons.OK, MessageBoxIcon.Information);
  return;
}

How to know if there is something like:

if (!nomeDoDataGridView.Controls.OfType<DataGridViewCheckBoxColumn>().Any(x => x.Checked))
  • You are using some Data Model in your Datagrid?

3 answers

2

I did an extension method for DataGridViewCheckBoxColumn, so you can use in a simpler way:

Method:

public static List<DataGridViewRow> CheckedRows(this DataGridViewCheckBoxColumn c)
{
    return c.DataGridView.Rows.Cast<DataGridViewRow>().Where(r => r.Cells[c.Name].Value != null && (bool)r.Cells[c.Name].Value).ToList();
}

Utilizing:

private void button1_Click(object sender, EventArgs e)
{
    var linhas = minhaCheckBoxColumn.CheckedRows();

    if (linhas.Count > 0)
    {
        //há linhas marcadas
    }
}

I preferred to return a list of DataGridViewRow so that the method can be used more widely.

1

If you have used an itemSoucer(model) to fill your Datagridview you can put a Biding for a certain field that in case would be your Checkbox

within your model create a new element called MARKED and do the checks In your model you can put something like this

 [System.ComponentModel.DataAnnotations.Schema.NotMapped]
 public bool MARCADO { get; set; }

And to serve the marked fields use this code below

// verifica se todos estão  marcados  
bool a = dgv.Controls.OfType<SeuModel>().All(x => x.MARCADO == true);

// verifica se todos estão  marcados desmarcados 
bool b = dgv.Controls.OfType<SeuModel>().All(x => x.MARCADO == false);

// verifica se tem  algum marcado
int c = dgv.Controls.OfType<SeuModel>().Where(x => x.MARCADO == false).Count();

The System.ComponentModel.DataAnnotations.Schema.NotMapped serves to ignore mapping if you are using Entity in your project.

for more details see this post

1

A simpler and perhaps more elegant form is:

var existeCheckBoxMarcado = MeuDataGridView.Rows.Cast<DataGridViewRow>().Any(row => Convert.ToBoolean(row.Cells[0].FormattedValue));

if (!existeCheckBoxMarcado)
{
    MessageBox.Show("Nenhuma Nota Selecionada", "A T E N Ç Ã O", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

Browser other questions tagged

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