DataGridView.CurrentCellDirtyStateChanged 事件
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
单元格状态因其内容更改而更改时发生。
public:
event EventHandler ^ CurrentCellDirtyStateChanged;
public event EventHandler CurrentCellDirtyStateChanged;
public event EventHandler? CurrentCellDirtyStateChanged;
member this.CurrentCellDirtyStateChanged : EventHandler
Public Custom Event CurrentCellDirtyStateChanged As EventHandler
事件类型
示例
下面的代码示例演示如何处理 CurrentCellDirtyStateChanged 事件。 在此示例中,事件处理程序调用 CommitEdit 方法以引发 CellValueChanged 事件并确定 的 DataGridViewCheckBoxCell当前值。 此代码示例是如何:禁用 Windows 窗体 DataGridView 控件中按钮列中的按钮中提供的更大示例的一部分。
// This event handler manually raises the CellValueChanged event
// by calling the CommitEdit method.
void dataGridView1_CurrentCellDirtyStateChanged(object sender,
EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
// If a check box cell is clicked, this event handler disables
// or enables the button in the same row as the clicked cell.
public void dataGridView1_CellValueChanged(object sender,
DataGridViewCellEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].Name == "CheckBoxes")
{
DataGridViewDisableButtonCell buttonCell =
(DataGridViewDisableButtonCell)dataGridView1.
Rows[e.RowIndex].Cells["Buttons"];
DataGridViewCheckBoxCell checkCell =
(DataGridViewCheckBoxCell)dataGridView1.
Rows[e.RowIndex].Cells["CheckBoxes"];
buttonCell.Enabled = !(Boolean)checkCell.Value;
dataGridView1.Invalidate();
}
}
' This event handler manually raises the CellValueChanged event
' by calling the CommitEdit method.
Sub dataGridView1_CurrentCellDirtyStateChanged( _
ByVal sender As Object, ByVal e As EventArgs) _
Handles dataGridView1.CurrentCellDirtyStateChanged
If dataGridView1.IsCurrentCellDirty Then
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
End If
End Sub
' If a check box cell is clicked, this event handler disables
' or enables the button in the same row as the clicked cell.
Public Sub dataGridView1_CellValueChanged(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) _
Handles dataGridView1.CellValueChanged
If dataGridView1.Columns(e.ColumnIndex).Name = "CheckBoxes" Then
Dim buttonCell As DataGridViewDisableButtonCell = _
CType(dataGridView1.Rows(e.RowIndex).Cells("Buttons"), _
DataGridViewDisableButtonCell)
Dim checkCell As DataGridViewCheckBoxCell = _
CType(dataGridView1.Rows(e.RowIndex).Cells("CheckBoxes"), _
DataGridViewCheckBoxCell)
buttonCell.Enabled = Not CType(checkCell.Value, [Boolean])
dataGridView1.Invalidate()
End If
End Sub
注解
如果单元格的内容已更改,但尚未保存更改,则会将其标记为已修改。
当单元格已编辑但更改尚未提交到数据缓存时,或者取消编辑操作时,通常会发生此事件。
有关如何处理事件的详细信息,请参阅 处理和引发事件。