DataGridView.IsCurrentCellDirty 属性

定义

获取一个值,该值指示当前单元格是否有未提交的更改。

public:
 property bool IsCurrentCellDirty { bool get(); };
[System.ComponentModel.Browsable(false)]
public bool IsCurrentCellDirty { get; }
[<System.ComponentModel.Browsable(false)>]
member this.IsCurrentCellDirty : bool
Public ReadOnly Property IsCurrentCellDirty As Boolean

属性值

如果当前单元格有未提交的更改,为 true;否则为 false

属性

示例

下面的代码示例使用 IsCurrentCellDirty 属性来确定是否提交单元格值并从事件的处理程序CurrentCellDirtyStateChanged引发 CellValueChanged 事件。 此代码示例是 DataGridView 控件中如何:禁用按钮列中的按钮Windows 窗体中提供的更大示例的一部分。

// 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

注解

DataGridView如果未绑定到数据,当用户移动到另一个单元格时,单元格编辑被视为已提交。

如果行标题可见,则包含未提交更改的单元格的行的页眉中会显示铅笔字形。

如果 IsCurrentCellDirtytrue 并且当前单元格承载编辑控件,则可以通过 属性检索它 EditingControl

适用于

另请参阅