Vb.net 如果用户单击该行并将焦点从该行移开,则重置DataGridView空行 如果用户单击DATAGIDVIEW底部的空白行并将焦点移离DATAGIDVIEW,则行单击现在处于表示对该行进行更改的状态。

Vb.net 如果用户单击该行并将焦点从该行移开,则重置DataGridView空行 如果用户单击DATAGIDVIEW底部的空白行并将焦点移离DATAGIDVIEW,则行单击现在处于表示对该行进行更改的状态。,vb.net,datagridview,focus,reset,Vb.net,Datagridview,Focus,Reset,是否可以告诉DataGridView将此行取消标记为已更改 当焦点离开DataGridView时,是否可以重置此行 我们正在使用以下事件处理程序来警告用户,如果发票日期为空: Private Sub dataGridViewPayments_CellValidating(ByVal sender As Object, _ ByVal e As DataGridViewCellValidatingEventArgs) _ Handles DataGridViewPayments.CellValid

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

当焦点离开DataGridView时,是否可以重置此行

我们正在使用以下事件处理程序来警告用户,如果发票日期为空:

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

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

您可以尝试以下操作:

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

请告诉我它是否有用

dgv的默认行为是仅在开始编辑新行时创建新行。除非修改了“开始编辑”行为,否则仅单击该行不应执行任何操作。是在您开始编辑之后,您得到了标记为要添加的行吗?不管怎样,这都不容易解决。最好查看新行的行验证或默认值,以便在网格失去焦点时确保行始终准备好保存。感谢David的回复。我认为我需要更改CellValidating事件处理程序。作为本文的一部分,我将在该处理程序中包含编码。如果用户只是在网格中单击,然后尝试单击表单上的其他位置,您可以告诉我如何添加到此处理程序以不验证吗?是的,它确实有帮助。谢谢。:-)