-
Mar 24th, 2018, 07:52 AM
#1
Thread Starter
Frenzied Member
DGV RowValidating causes Error
Hi..
In the DGV i have two Columns named ColQty and ColFree along with other relevant columns. It is required either of the one should have an value. To do so i have used the following code, which gives an error as
"Operation did not succeed because the program cannot commit or quit a cell value change."
vb Code:
Private Sub DataGridView1_RowValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DataGridView1.RowValidating
If FormLoaded AndAlso e.RowIndex > -1 Then
If Not Me.DataGridView1.CurrentRow Is Nothing Then
Dim ContinueChecking As Boolean = True
Dim r As DataGridViewRow = Me.DataGridView1.CurrentRow
If (Not r.Cells(Me.ColProductCode.Index).Value Is Nothing) AndAlso (Not r.Cells(Me.ColProductCode.Index).Value Is DBNull.Value) Then
Dim qty As Decimal = 0
Dim free As Decimal = 0
If (Not r.Cells(Me.ColQty.Index).Value Is Nothing) AndAlso (Not r.Cells(Me.ColQty.Index).Value Is DBNull.Value) Then
qty = CDec(r.Cells(Me.ColQty.Index).Value)
End If
If (Not r.Cells(Me.ColFree.Index).Value Is Nothing) AndAlso (Not r.Cells(Me.ColFree.Index).Value Is DBNull.Value) Then
free = CDec(r.Cells(Me.ColFree.Index).Value)
End If
If qty + free = 0 Then
MessageBox.Show("Enter the Product Quantity", MsgboxHeader, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
e.Cancel = True
ContinueChecking = False
End If
If ContinueChecking Then
'Gotta do the Same check for Rates..
End If
End If
End If
End If
After Googling,I beleive that the problem is with setting e.Cancel = True. They suggest to set e.cancel = false. But it doesn't set the purpose.
Plz Help.
Regards
-
Mar 24th, 2018, 09:50 AM
#2
Re: DGV RowValidating causes Error
You should not be using CurrentRow in that code. The event handler will tell you which row is being validated (that's what e.RowIndex is for) and you should use that. I'd suggest making that change and see if the issue goes away. If not, I'll try some tests of my own to see if I can reproduce your issue.
-
Mar 25th, 2018, 04:04 AM
#3
Thread Starter
Frenzied Member
Re: DGV RowValidating causes Error
No...I still get the same error
-
Aug 5th, 2018, 05:47 AM
#4
Thread Starter
Frenzied Member
Re: DGV RowValidating causes Error
Still stuck on this..plz help
-
Aug 5th, 2018, 06:21 AM
#5
Re: DGV RowValidating causes Error
I just tested this code and it worked exactly as expected:
vb.net Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim table As New DataTable With table.Columns .Add("ID", GetType(Integer)).AutoIncrement = True .Add("Required", GetType(String)) .Add("NotRequired", GetType(String)) .Add("ConditionallyRequired1", GetType(String)) .Add("ConditionallyRequired2", GetType(String)) End With DataGridView1.DataSource = table End Sub Private Sub DataGridView1_RowValidating(sender As Object, e As DataGridViewCellCancelEventArgs) Handles DataGridView1.RowValidating Dim row = DataGridView1.Rows(e.RowIndex) If row.Cells(1).Value Is DBNull.Value Then e.Cancel = True MessageBox.Show("You must enter a value in column 'Required'.") End If If row.Cells(3).Value Is DBNull.Value AndAlso row.Cells(4).Value Is DBNull.Value Then e.Cancel = True MessageBox.Show("You must enter a value in at least one of columns 'ConditionallyRequired1' and 'ConditionallyRequired2'.") End If End Sub
If I left the second column empty or both the fourth and fifth columns empty then I received an error message (or two if I did both) and focus remained on that row.
-
Aug 5th, 2018, 06:43 AM
#6
Thread Starter
Frenzied Member
Re: DGV RowValidating causes Error
Well my bad, I must have specified it before..
When the user press the Enter key i wish to set the focus back to column1 of the next row.
To do so, i am Handling the Keydown event of the DGV and and setting the CurrentCell value...Thats where i am getting the error..
-
Aug 5th, 2018, 08:50 AM
#7
Re: DGV RowValidating causes Error
If you want help with a specific code issue, I'd recommend posting that code.
-
Aug 7th, 2018, 09:58 AM
#8
Thread Starter
Frenzied Member
Re: DGV RowValidating causes Error
This is the code..
vb Code:
Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown If e.KeyCode = Keys.Enter Then If (Not Me.DataGridView1.CurrentRow Is Nothing) AndAlso Me.DataGridView1.CurrentRow.Index <> Me.DataGridView1.RowCount - 1 Then Me.DataGridView1.CurrentCell = Me.DataGridView1(0, Me.DataGridView1.CurrentRow.Index + 1) End If End If End Sub
-
Aug 9th, 2018, 05:17 AM
#9
Thread Starter
Frenzied Member
Re: DGV RowValidating causes Error
The Error occurs only when the Row validation fails.
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
|