Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to find a way to prevent a user from exiting a datagridviewcell if they provide the wrong input. For the column in question I'm looking for a way to prevent the user from entering negative numbers or leaving the datagridviewcell blank, provide a meaningful messagebox. Also I'm hoping that after the messagebox appears to prevent the user from leaving the cell until the correct input is completed. I've found ways of doing all of these things, but not at the same time. Here is my code below. If anyone has any suggestions or answers on how to get this working, I would greatly appreciate it!

VB
Private Sub DataGridView1_CellValidating(sender As Object, e As          DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
    If (e.ColumnIndex = 2) Then
        Dim cellData = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
        If cellData Is Nothing OrElse IsDBNull(cellData) OrElse cellData.ToString = String.Empty Then
            MessageBox.Show("Cannot Be Empty")
            e.Cancel = True
        ElseIf cellData <= 0 Then
            MessageBox.Show("Negative Numbers and Zero Not Allowed")
            e.Cancel = True
            Exit Sub



        End If

    End If
End Sub
Posted
Comments
Foothill 2-Jul-13 10:06am    
I guess you can make the form modal, preventing the user from leaving that form.

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