I have been struggling with two DataGridView issues. My DataGridView is bound to a DataTable via a BindingSource.
The first issue that I am having trouble with is moving the selected cell after I cancel an edit. I am trying to do this at the end of the below code listing ("If flgCancel Then..."). What happens is the highlighted cell remains at the row/column of the cell with the cancelled edit.
I use the CellValidating event of the DataGridView as follows:
The second issue that I'm having trouble figuring out is when cancelling an edit to a new row in the grid, the above code does not move up to the last existing row in the grid, and then the BindingSource.AddingNew event fires. What I want to do when cancelling the edits to a new row is put the focus back on an existing row and stop trying to add any more new rows. How do I prevent the AddingNew event from firing in this scenario?Code:Private Sub dgvActions_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles dgvActions.CellValidating 'https://msdn.microsoft.com/en-us/library/ykdxa0bc.aspx#Y400 'http://stackoverflow.com/questions/20756463/restoring-previous-cell-value-in-c-sharp-data 'dgvActions.CancelEdit() '...clear any errors associated with the cell 'dgvActions.EndEdit() '...the MSDN documentation is incorrect and the public overload of CancelEdit does not exit edit mode and to do so we need to follow it with EndEdit 'https://social.msdn.microsoft.com/Forums/en-US/84c06fbe-5c55-42b0-bc2d-6096c2720b4d/datagridviewcanceledit-doesnt?forum=winformsdatacontrols Dim strMethodName = New System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name '...this procedure's name Dim flgDebug As Boolean = False '...debug/test purposes only Dim flgCancel As Boolean = False Try If dgvActions.Columns(e.ColumnIndex).Name = "ActionName" Then If String.IsNullOrEmpty(e.FormattedValue.ToString()) Then If MessageBox.Show("Action cannot not be blank, empty, or a zero-length string (ZLS)." & vbCrLf & vbCrLf & _ "Cancel edits or retry editing?", "Action", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Cancel Then flgCancel = True Else e.Cancel = True dgvActions.CurrentRow.Cells("ActionName").Selected = True If Not IsNothing(dgvActions.Rows(e.RowIndex).Cells("ActionName").Tag) Then dgvActions.Rows(e.RowIndex).Cells("ActionName").Value = dgvActions.Rows(e.RowIndex).Cells("ActionName").Tag End If End If End If ElseIf dgvActions.Columns(e.ColumnIndex).Name = "FinishDate" Then If e.FormattedValue.ToString() <> "" Then If Not IsDate(e.FormattedValue.ToString()) Then If MessageBox.Show("Finish date is not a valid date." & vbCrLf & vbCrLf & _ "Cancel edits or retry editing?", "Action", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Cancel Then flgCancel = True Else e.Cancel = True dgvActions.CurrentRow.Cells("FinishDate").Selected = True If Not IsNothing(dgvActions.Rows(e.RowIndex).Cells("FinishDate").Tag) Then dgvActions.Rows(e.RowIndex).Cells("FinishDate").Value = dgvActions.Rows(e.RowIndex).Cells("FinishDate").Tag End If End If End If End If End If If flgCancel Then Dim priorRowIndex As Integer = e.RowIndex - 1 dgvActions.CancelEdit() '...clear any errors associated with the cell dgvActions.EndEdit() '...the MSDN documentation is incorrect and the public overload of CancelEdit does not exit edit mode and to do so we need to follow it with EndEdit dgvActions.Rows(priorRowIndex).Cells(e.ColumnIndex).Selected = True bsActions.Position = priorRowIndex _FormDirty = IsBindingSourceDirty(bsActions) btnSave.Enabled = _FormDirty btnCancel.Enabled = _FormDirty End If Catch ex As Exception MessageBox.Show(ex.GetType.ToString & vbCrLf & vbCrLf & ex.Message & " (err=" & Err.Number & ")", strMethodName, MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub
What should I do to make this happen? Appreciate any advice...thanks!




Reply With Quote
