Results 1 to 9 of 9

Thread: DGV RowValidating causes Error

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,148

    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:
    1. Private Sub DataGridView1_RowValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DataGridView1.RowValidating
    2.         If FormLoaded AndAlso e.RowIndex > -1 Then
    3.             If Not Me.DataGridView1.CurrentRow Is Nothing Then
    4.                 Dim ContinueChecking As Boolean = True
    5.  
    6.                 Dim r As DataGridViewRow = Me.DataGridView1.CurrentRow
    7.  
    8.                 If (Not r.Cells(Me.ColProductCode.Index).Value Is Nothing) AndAlso (Not r.Cells(Me.ColProductCode.Index).Value Is DBNull.Value) Then
    9.                     Dim qty As Decimal = 0
    10.                     Dim free As Decimal = 0
    11.  
    12.                     If (Not r.Cells(Me.ColQty.Index).Value Is Nothing) AndAlso (Not r.Cells(Me.ColQty.Index).Value Is DBNull.Value) Then
    13.                         qty = CDec(r.Cells(Me.ColQty.Index).Value)
    14.                     End If
    15.  
    16.                     If (Not r.Cells(Me.ColFree.Index).Value Is Nothing) AndAlso (Not r.Cells(Me.ColFree.Index).Value Is DBNull.Value) Then
    17.                         free = CDec(r.Cells(Me.ColFree.Index).Value)
    18.                     End If
    19.  
    20.                     If qty + free = 0 Then
    21.                         MessageBox.Show("Enter the Product Quantity", MsgboxHeader, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    22.                         e.Cancel = True
    23.                         ContinueChecking = False
    24.                     End If
    25.  
    26.  
    27.                     If ContinueChecking Then
    28.                               'Gotta do the Same check for Rates..
    29.  
    30.                     End If
    31.  
    32.                 End If
    33.             End If
    34.         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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,305

    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.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,148

    Re: DGV RowValidating causes Error

    No...I still get the same error

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,148

    Re: DGV RowValidating causes Error

    Still stuck on this..plz help

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,305

    Re: DGV RowValidating causes Error

    I just tested this code and it worked exactly as expected:
    vb.net Code:
    1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    2.     Dim table As New DataTable
    3.  
    4.     With table.Columns
    5.         .Add("ID", GetType(Integer)).AutoIncrement = True
    6.         .Add("Required", GetType(String))
    7.         .Add("NotRequired", GetType(String))
    8.         .Add("ConditionallyRequired1", GetType(String))
    9.         .Add("ConditionallyRequired2", GetType(String))
    10.     End With
    11.  
    12.     DataGridView1.DataSource = table
    13. End Sub
    14.  
    15. Private Sub DataGridView1_RowValidating(sender As Object, e As DataGridViewCellCancelEventArgs) Handles DataGridView1.RowValidating
    16.     Dim row = DataGridView1.Rows(e.RowIndex)
    17.  
    18.     If row.Cells(1).Value Is DBNull.Value Then
    19.         e.Cancel = True
    20.  
    21.         MessageBox.Show("You must enter a value in column 'Required'.")
    22.     End If
    23.  
    24.     If row.Cells(3).Value Is DBNull.Value AndAlso row.Cells(4).Value Is DBNull.Value Then
    25.         e.Cancel = True
    26.  
    27.         MessageBox.Show("You must enter a value in at least one of columns 'ConditionallyRequired1' and 'ConditionallyRequired2'.")
    28.     End If
    29. 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.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,148

    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..

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,305

    Re: DGV RowValidating causes Error

    If you want help with a specific code issue, I'd recommend posting that code.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,148

    Re: DGV RowValidating causes Error

    This is the code..

    vb Code:
    1. Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
    2.         If e.KeyCode = Keys.Enter Then
    3.             If (Not Me.DataGridView1.CurrentRow Is Nothing) AndAlso Me.DataGridView1.CurrentRow.Index <> Me.DataGridView1.RowCount - 1 Then
    4.                 Me.DataGridView1.CurrentCell = Me.DataGridView1(0, Me.DataGridView1.CurrentRow.Index + 1)
    5.             End If
    6.         End If
    7.     End Sub

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,148

    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
  •  



Click Here to Expand Forum to Full Width