user12799076
user12799076

Reputation:

Check control in DataGridView

I am needing that if there is no CheckBox selected in the DataGridView the button Uno and the button Varios are disabled. If a single CheckBox is selected, the button Uno is enabled and the button Varios disabled. And if there is more than one CheckBox selected, the button Uno is disabled and the button Varios is enabled.

But, the following happens:

enter image description here

The code I use is the following:

public Form1()
{
    InitializeComponent();

    btnUno.Enabled = false;
    btnVarios.Enabled = false;
}

To enable and disable the buttons:

private void dtgTitulo_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    int contador = 0;
    foreach (DataGridViewRow row in dtgTitulo.Rows)
    {
        if (row.Cells["Seleccione"].Value != null && row.Cells["Seleccione"].Value.Equals(true))//Columna de checks
        {
            contador++;
            if (contador <= 0)
            {
                btnUno.Enabled = false;
                btnVarios.Enabled = false;
            }
            else if (contador == 1)
            {
                btnUno.Enabled = true;
                btnVarios.Enabled = false;
            }
            else
            {
                btnUno.Enabled = false;
                btnVarios.Enabled = true;
            }
        }
    }
}

Can someone help me? Any suggestion?

UPDATE HOW TO I LOAD THE DATAGRIDVIEW WITH CHECKBOXES:

private DataTable Query()
        {
            DataTable datos = new DataTable();
            SqlConnection sqlConn = new SqlConnection("STRING");
            try
            {
                sqlConn.Open();
                string consulta = "SELECT Titulo AS Título FROM V_CuetaWeb GROUP BY titulo ORDER BY titulo DESC";
                SqlCommand sqlCommand = new SqlCommand(consulta, sqlConn);
                SqlDataAdapter da = new SqlDataAdapter(sqlCommand);//este se encarga de inicializar el command
                da.Fill(datos);
                sqlConn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return datos;
        }

I form_Load:

private void Form1_Load(object sender, EventArgs e)
        {
            ds = new DataSet(); 
            ds.Tables.Add(Query());
            ds.Tables[0].Columns.Add("Seleccione", typeof(bool));
            dtgTitulo.DataSource = ds.Tables[0];
        }

Upvotes: 0

Views: 74

Answers (2)

Lucifer
Lucifer

Reputation: 1594

Try Below code:

AS per @JIMI's suggestion

private void dtgTitulo_CellMouseUp(object sender, DataGridViewCellEventArgs e)
{
     if (e.ColumnIndex != 1) return; 

     dtgTitulo.CommitEdit(DataGridViewDataErrorContexts.Commit); 

     var contador  = dtgTitulo.Rows.OfType<DataGridViewRow>().Count(r => (r.Cells[1].Value != null) && ((bool)r.Cells[1].Value == true));

     if (contador <= 0)
     {
          btnUno.Enabled = false;
          btnVarios.Enabled = false;
     }
     else
     {
          if (contador == 1)
          {
              btnUno.Enabled = true;
              btnVarios.Enabled = false;
          }
          else
          {
              btnUno.Enabled = false;
              btnVarios.Enabled = true;
          }
     }
}

Upvotes: 1

ASh
ASh

Reputation: 35681

after click on checkboxes they show/hide ticks, but value in cell doesn't change immediately. call EndEdit to apply them.

private void dtgTitulo_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex < 0 || dtgTitulo.Columns[e.ColumnIndex].Name != "Seleccione")
        return;
    dtgTitulo.EndEdit();

    int contador = 0;
    foreach (DataGridViewRow row in dtgTitulo.Rows)
    {
        if (Equals(true, row.Cells["Seleccione"].Value))
        {
            contador++;
            if (contador > 1)
                break;
        }
    }

    btnUno.Enabled = contador == 1;
    btnVarios.Enabled = contador > 1;
}

p.s. note optimizations made to avoid unnecessary iterations

Upvotes: 0

Related Questions