none
DataGridView CellValidating RRS feed

  • Question

  • I'm using DataGridView in Form1. If Column(6)>2 then, Show MsgBox "Out of Order" then focus to current cell. How to write this code. My code is :

    Private Sub DGV_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles DGV.CellValidating
    
       If (e.ColumnIndex = 6) Then
          If DGV.CurrentRow.Cells(6).Value > 2 Then Return
             e.Cancel = True
             MsgBox("Out of Order")
         DGV.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 6
    
       End If
    End Sub

    Tuesday, June 6, 2017 4:06 AM

All replies

  • Hi Dopen,

    According to your description, you can refer to the code below.

     Private Sub DataGridView1_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
            Dim dgv As DataGridView = CType(sender, DataGridView)
            If dgv.Columns(e.ColumnIndex).Name = "Column6" AndAlso
            e.FormattedValue > 2 Then
                e.Cancel = True
                MessageBox.Show("Out of Order")
            End If
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 6, 2017 9:12 AM
    Moderator
  • You are close.  The column with index #6 is the seventh column - the first column is index 0.  So the sixth column is index 5.  Also, you probably want to handle the CellValueChanged event instead of the Validating event due to the way in which the events are raised.

        Private Sub DGV_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DGV.CellValueChanged
            If e.RowIndex > -1 AndAlso (e.ColumnIndex = 5) Then
                Dim valueObj As Object = DGV.CurrentRow.Cells(5).Value
                If valueObj IsNot Nothing AndAlso (CInt(valueObj)) > 2 Then Return
                MsgBox("Out of Order")
                DGV.Rows(e.RowIndex).Cells(5).Value = 6
            End If
        End Sub


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, June 7, 2017 7:49 PM
    Moderator
  • Hi Dopen,

    If these reply have resolved your issue, please remember to close your threads by marking helpful posts as answer, it is very beneficial to the other communities who face the same issue.

    Thanks for your understanding.

    Best Regards,

    Cherry



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 12, 2017 1:34 AM
    Moderator