0

I want to fire an event when any of the datagridviewcheckbox is checked. I tried the foreach but it only trigger when all the datagridviewcheck is checked. I want to fire an event if any of the datagridviewcheckboxcell is checked.

foreach (DataGridViewRow row in dgvLocal.Rows)
{
    if ((Convert.ToBoolean(row.Cells[0].Value) == true))
    {
        //
    }               
}
4
  • 1
    What is "triggering" this foreach? I think we're missing some context here. Commented Sep 24, 2018 at 12:40
  • That code looks like it will do something when it finds the first item that's checked, you only need a break inside the if so it stops checking when one is found. If you have that, then there must be something else. Commented Sep 24, 2018 at 12:41
  • I dont know which event will i put this code on. Commented Sep 24, 2018 at 12:47
  • I just want to show a messagebox when I check any checkboxcell in datagridview. Commented Sep 24, 2018 at 12:49

3 Answers 3

1

Use the cellcontentclicked event of the datagridview Also use the CurrentCellDirtystateChanged to make sure the last click is commited

  void grd_CurrentCellDirtyStateChanged(object sender, EventArgs e)
            {
                if (grd.IsCurrentCellDirty)
                    grd.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }


    private void grd_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {


            if (e.ColumnIndex == 1) //compare to checkBox column index
            {
                DataGridViewCheckBoxCell cbx = (DataGridViewCheckBoxCell)grd[e.ColumnIndex, e.RowIndex];
                if (!DBNull.Value.Equals(cbx.Value) && (bool)cbx.Value == true)
                {
                    //checkBox is checked - do the code in here!
                }
                else
                {
                    //if checkBox is NOT checked (unchecked)
                }
            }
        }
Sign up to request clarification or add additional context in comments.

Comments

0

Try this out

foreach (DataGridViewRow row in dataGridView1.Rows)
{
 DataGridViewCheckBoxCell check = (DataGridViewCheckBoxCell)row.Cells[1];
 if (check.Value  == check.TrueValue)
  {
    //dosomething
  }
 else
  {
   //dosomething
  }
}

2 Comments

Error 5 Operator '==' cannot be applied to operands of type 'object' and 'bool' C:\Users\MOMO\Desktop\MLQ System 9.26\MLQ SYTSTEM 9.26\MLQSystem\UpdateVisitors.cs 924 21 MLQSystem
try to use '=' instead of '=='
0

Trigger CellValueChanged

private void dgvProducts_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        if (dgvProducts.DataSource != null)
        {
            if (dgvProducts.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "True")
            {
                //do something
            }
            else
            {
               //do something
            }
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.