【发布时间】:2017-06-22 13:54:25
【问题描述】:
这似乎是一些非常基本的功能,但我在 StackOverflow 或文档中找不到任何具体示例。
检查 DataGridView 的事件似乎没有任何可用的东西可以直接监视复选框单元格中的更改。
谁能提供一个监控datagridview中的复选框检查事件然后执行方法的示例?
【问题讨论】:
标签: c# winforms events datagridview
这似乎是一些非常基本的功能,但我在 StackOverflow 或文档中找不到任何具体示例。
检查 DataGridView 的事件似乎没有任何可用的东西可以直接监视复选框单元格中的更改。
谁能提供一个监控datagridview中的复选框检查事件然后执行方法的示例?
【问题讨论】:
标签: c# winforms events datagridview
经过一番搜索后,我发现获取更改值的最佳方法是检查 CurrentCellDirtyStateChanged 并触发编辑并检查单元格当前值:
private void DataGrid_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (DataGrid.Columns[e.ColumnIndex].Name == "colReserved")
{
DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)DataGrid.Rows[e.RowIndex].Cells["colReserved"];
if ((Boolean)checkCell.Value)
{
//Checked
MessageBox.Show("Checked");
}
else
{
//Not Checked
MessageBox.Show("UnChecked");
}
DataGrid.Invalidate();
}
}
private void DataGrid_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (DataGrid.IsCurrentCellDirty)
{
DataGrid.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
【讨论】: