-
Feb 4th, 2017, 06:49 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] DataGridView cancel new row addition and move to last row of grid
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:
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
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?
What should I do to make this happen? Appreciate any advice...thanks!
-
Feb 4th, 2017, 07:52 PM
#2
Re: DataGridView cancel new row addition and move to last row of grid
I haven't read all that code but I suspect that some of it at least doesn't belong in the CellValidating event handler. The purpose of that event is to allow you to validate the contents of the cell that the user is trying to leave. If you're doing anything beyond that then you're misusing the event. You are supposed to test the contents of the cell and set e.Cancel to True if it's not valid, which will prevent the cell losing focus. That's it, that's all. If you want to do anything more than that then you should look at the CellValidated and/or CellLeave events.
-
Feb 5th, 2017, 01:01 PM
#3
Thread Starter
Hyperactive Member
Re: DataGridView cancel new row addition and move to last row of grid
Thank you, JMC. Appreciate all your help!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|