4

如果用户单击 DataGridView 底部的空白行并将焦点从 DataGridView 移开,则单击的行现在处于指示对该行进行更改的状态。

是否可以告诉 DataGridView 取消将此行标记为已更改?

焦点不在 DataGridView 时是否可以重置此行?

如果 Invoiced On 留空,我们将使用以下事件处理程序来提醒用户:

Private Sub dataGridViewPayments_CellValidating(ByVal sender As Object, _
ByVal e As DataGridViewCellValidatingEventArgs) _
Handles DataGridViewPayments.CellValidating

    Dim headerText As String = _
        DataGridViewPayments.Columns(e.ColumnIndex).HeaderText

    ' Validate the Invoiced On cell and display the error if it's empty.
    '-------------------------------------------------------------------
    If (String.IsNullOrEmpty(e.FormattedValue.ToString()) And
        headerText.Equals("Invoiced On")) Then

        DataGridViewPayments.Rows(e.RowIndex).ErrorText = _
            "Please enter an Inoiced On date."

        e.Cancel = True
    End If
End Sub

如果用户只是单击网格然后单击表单中的其他位置,看起来我们需要一种方法来阻止它执行。

4

1 回答 1

1

你可以尝试这样的事情:

Private Sub dg_CellValidating(ByVal sender As Object, ByVal e As DataGridViewCellValidatingEventArgs) Handles dg.CellValidating
    Dim headerText As String = dg.Columns(e.ColumnIndex).HeaderText

    'Try this --------------------------------------------------------------
    Dim vClicked As Boolean = False
    If (Control.MouseButtons And MouseButtons.Left) = MouseButtons.Left Then
        vClicked = True
    End If
    '-----------------------------------------------------------------------

    'Validate the Invoiced On cell and display the error if it's empty.
    'And put vClicked here
    If Not vClicked AndAlso (String.IsNullOrEmpty(e.FormattedValue.ToString()) And headerText.Equals("Invoiced On")) Then

        dg.Rows(e.RowIndex).ErrorText = "Please enter an Inoiced On date."

        e.Cancel = True
    End If
End Sub

请让我知道它是否有帮助。=)

于 2012-06-20T13:19:56.057 回答