C# WinForms中的DataGridViewCheckBox单元格检查事件
我有未绑定的C# WinForms中的DataGridViewCheckBox单元格检查事件,c#,.net,winforms,datagridview,C#,.net,Winforms,Datagridview,我有未绑定的DataGridViewCheckBox列。当用户更改检查状态时,我需要执行方法来检查每一行的状态,查找复选框列,并基于该检查状态对其他列进行一些计算 到目前为止,我尝试过的每个事件:CellContentClick、CellMouseUp、cellvalized、CellValueChanged在我离开单元格或抓取DataGridViewCheckBox`检查更改操作之前的状态 什么事件允许在更改后但在我离开单元格或行之前获取值?首先需要设置DataGridViewCheckBox
DataGridViewCheckBox
列。当用户更改检查状态时,我需要执行方法来检查每一行的状态,查找复选框列,并基于该检查状态对其他列进行一些计算
到目前为止,我尝试过的每个事件:CellContentClick
、CellMouseUp、
cellvalized、
CellValueChanged在我离开单元格或抓取
DataGridViewCheckBox`检查更改操作之前的状态
什么事件允许在更改后但在我离开单元格或行之前获取
值?首先需要设置DataGridViewCheckBoxColumn TrueValue和FalseValue属性。然后,您应该处理DataGridView CellContentClick和CellValueChanged,以确定是否选中该单元格
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
var checkBoxColumn = (DataGridViewCheckBoxColumn)this.dataGridView1.Columns[0];
checkBoxColumn.TrueValue = true;
checkBoxColumn.FalseValue = false;
this.dataGridView1.CellContentClick += new DataGridViewCellEventHandler(dataGridView1_CellContentClick);
this.dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
DataGridViewCheckBoxCell cell = (sender as DataGridView).Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewCheckBoxCell;
if (cell != null)
{
if (cell.Value == cell.TrueValue)
{
MessageBox.Show("Cell checked.");
}
else
{
MessageBox.Show("Cell unchecked.");
}
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
var dataGridView = sender as DataGridView;
if (dataGridView.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn)
{
// Raise CellValueChanged
dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
}