How to detect DataGridView CheckBox event change?

后端 未结 16 2478
离开以前
离开以前 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:35

    In the event CellContentClick you can use this strategy:

    private void myDataGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {    
        if (e.ColumnIndex == 2)//set your checkbox column index instead of 2
        {   //When you check
            if (Convert.ToBoolean(myDataGrid.Rows[e.RowIndex].Cells[2].EditedFormattedValue) == true)
            {
                //EXAMPLE OF OTHER CODE
                myDataGrid.Rows[e.RowIndex].Cells[5].Value = DateTime.Now.ToShortDateString();
    
                //SET BY CODE THE CHECK BOX
                myDataGrid.Rows[e.RowIndex].Cells[2].Value = 1;
            }
            else //When you decheck
            {
                myDataGrid.Rows[e.RowIndex].Cells[5].Value = String.Empty;
    
                //SET BY CODE THE CHECK BOX
                myDataGrid.Rows[e.RowIndex].Cells[2].Value = 0;
            }
        }
    }
    

提交回复
热议问题