How to detect DataGridView CheckBox event change?

后端 未结 16 2379
离开以前
离开以前 2020-11-27 12:43

I have a winforms app and want to trigger some code when a checkbox embedded in a DataGridView control is checked / unchecked. Every event I have tried either

16条回答
  •  醉梦人生
    2020-11-27 13:27

    The Code will loop in DataGridView and Will check if CheckBox Column is Checked

    private void dgv1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.ColumnIndex == 0 && e.RowIndex > -1)
        {
            dgv1.CommitEdit(DataGridViewDataErrorContexts.Commit);
            var i = 0;
            foreach (DataGridViewRow row in dgv1.Rows)
            {
                if (Convert.ToBoolean(row.Cells[0].Value))
                {
                    i++;
                }
            }
    
            //Enable Button1 if Checkbox is Checked
            if (i > 0)
            {
                Button1.Enabled = true;
            }
            else
            {
                Button1.Enabled = false;
            }
        }
    }
    

提交回复
热议问题