Reset Empty DataGridView field, if the user clicks in this row and removes focus from this row
If the user clicks an empty row at the bottom of the DataGridView and moves the focus away from the DataGridView, now the row click is in a state indicating that changes to this row have been made.
Can I tell the DataGridView to cancel this row as changed?
Is it possible to reset this line when the focus is disconnected from the DataGridView?
We use the following event handler to alert the user if Invoiced On remains empty:
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 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
It seems like we need a way to stop this from being executed if the user just clicks on the grid and then clicks elsewhere on the form.
+4
Emad-ud-deen
source share1 answer
You can try something like this:
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 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
Please let me know if this helped. =)
+1
felipebueno
source share