C# DataGridView Checkbox checked event

前端 未结 1 1695
迷失自我
迷失自我 2020-11-28 16:23

I want to handle Checked event of CheckBox columns in my DataGridView and perform an operation based on column checked value (true/fal

相关标签:
1条回答
  • 2020-11-28 17:02

    You can handle CellContentClick event of your DataGridView and put the logic for changing those cells there.

    The key point is using CommitEdit(DataGridViewDataErrorContexts.Commit) to commits changes in the current cell to the data cache without ending edit mode. This way when you check for value of cell in this event, it returns current checked or unchecked value which you see in the cell currently after click:

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        //We make DataGridCheckBoxColumn commit changes with single click
        //use index of logout column
        if(e.ColumnIndex == 4 && e.RowIndex>=0)
            this.dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
    
        //Check the value of cell
        if((bool)this.dataGridView1.CurrentCell.Value == true)
        {
            //Use index of TimeOut column
            this.dataGridView1.Rows[e.RowIndex].Cells[3].Value = DateTime.Now;
    
            //Set other columns values
        }
        else
        {
            //Use index of TimeOut column
            this.dataGridView1.Rows[e.RowIndex].Cells[3].Value = DBNull.Value;
    
            //Set other columns values
        }
    }
    
    0 讨论(0)
提交回复
热议问题