Click here to Skip to main content
15,889,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 3 columns

* Item Name
* Quantity on Hand
* Quantity to transfer

the item name and the qty on hand is diplayed to the user . If he tries to input a number which is more than the Qty he has on Hand; i need a messageBox to pop up...



However the event is not firing the exact time I want. It allows me to leave the row without the message popping up.

What I have tried:

Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating

With DataGridView1.Rows(e.RowIndex)
If e.ColumnIndex = 2 Then '' this is the col index for qty to be transfered
If .Cells(2).Value > .Cells(1).Value Then
MessageBox.Show("You don't Have sufficient quantity ", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
e.Cancel = True
End If
End If
End With
end sub
Posted
Updated 13-Aug-16 21:16pm

VB
Private Sub DataGridView1_RowValidating(sender As Object, e As DataGridViewCellCancelEventArgs) Handles DataGridView1.RowValidating
       Dim transferredQTY, onHandQTY As Double
       With DataGridView1.Rows(e.RowIndex)

           Try
               transferredQTY = Convert.ToDouble(.Cells(2).Value)
           Catch ex As Exception
               transferredQTY = 0
           End Try
           Try
               onHandQTY = Convert.ToDouble(.Cells(1).Value)
           Catch ex As Exception
               transferredQTY = 0
           End Try
           If transferredQTY > onHandQTY Then
               MessageBox.Show("You don't Have sufficient quantity ", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
               e.Cancel = True
           End If

       End With
   End Sub
 
Share this answer
 
Thanks J.O.V.Y... I think i found out what was the problem. The value wasn't written to the cell yet, so i used the value that is being passed from the event and that solved my problem

If Convert.ToInt32(e.FormattedValue) > Convert.ToInt32(.Cells(1).Value) Then
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900