locked
Datagridview and the cellvalidating event RRS feed

  • Question

  • I have a datagridview that allows the user to enter data into two of the columns in the dataviewgid.  As the data is entered, it is validated in the cellvalidating event of the dataviewgrid.  If an error occurs, a message box displays and the cell containing the erroneous data is highlighted.  The user clicks the OK button in the message box and they are returned to the dataviewgrid.  This seems to work fine.  But if the user clicks on the button to return to the previous form without correcting the erroneous data, the cellvalidating event executes again and the message box displays again.  Seems I'm caught in a loop.  What needs to be done to recognize the button being clicked?

    Tuesday, August 29, 2017 5:48 PM

Answers

  • Hi

    Why bother to validate if you then go on to negate the validation? The real solution is to not let the user leave the DGV without correcting the invalid data (or, if permissable, leave the entry blank)


    Regards Les, Livingston, Scotland

    • Marked as answer by Stephen Spohn Wednesday, August 30, 2017 1:57 PM
    Tuesday, August 29, 2017 6:22 PM

All replies

  • I'd say the easiest is to have a boolean variable indicating if the cell validating event for that particular cell has already been visited, and another boolean indicating that the back button has been pressed.

    Tuesday, August 29, 2017 6:19 PM
  • Hi

    Why bother to validate if you then go on to negate the validation? The real solution is to not let the user leave the DGV without correcting the invalid data (or, if permissable, leave the entry blank)


    Regards Les, Livingston, Scotland

    • Marked as answer by Stephen Spohn Wednesday, August 30, 2017 1:57 PM
    Tuesday, August 29, 2017 6:22 PM
  • Hi

    Here is some code that may offer some ideas. This example has a default DataGridView with 4 default Columns (Column1 ...... Column4)

    Column1 - will strip out any numeric characters and leave the string

    Column2 - will blank the cell if not a valid Integer

    Column3 - will blank the cell if not a valid Double

    Column4 - will accept anything (default)

    ' Form1 with a DGV with 4 default columns
    Option Strict On
    Option Infer Off
    Option Explicit On
    Public Class Form1
        Private Sub DataGridView1_CellValidating(ByVal sender As System.Object, ByVal e As DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
            Dim cell As DataGridViewCell = DataGridView1.Item(e.ColumnIndex, e.RowIndex)
            If cell.IsInEditMode Then
                Dim cellV As Control = DataGridView1.EditingControl
                Select Case DataGridView1.Columns(e.ColumnIndex).Name
                    Case "Column1"
                        cellV.Text = System.Text.RegularExpressions.Regex.Replace(cellV.Text, "[0-9\b\s-]", "")
                    Case "Column2"
                        Dim v As Integer = 0
                        If Not Integer.TryParse(cellV.Text, v) Then
                            cellV.Text = Nothing
                        End If
                    Case "Column3"
                        Dim v As Double = 0
                        If Not Double.TryParse(cellV.Text, v) Then
                            cellV.Text = Nothing
                        End If
                End Select
            End If
        End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, August 29, 2017 7:34 PM
    Tuesday, August 29, 2017 7:34 PM
  • Hi Stephen,

    According to your description, you want to return the previous cell in datagridview when you don't correct the erroneous data. I think you could erase the content of the cell currently in edit mode firstly, and set one correct value to this cell, then you can return the previous cell, like this following code:

     Private Sub Form9_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim dt As New DataTable
            dt.Columns.Add("ID", GetType(Int32))
            dt.Columns.Add("ProductName", GetType(String))
            dt.Columns.Add("Count", GetType(Int32))
            dt.Columns.Add("UnitPrice", GetType(Decimal))
            dt.Rows.Add(1, "AA", 12, 2.4)
            dt.Rows.Add(2, "BB", 24, 1.23)
            DataGridView1.DataSource = dt
        End Sub
    
        Private Sub DataGridView1_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
            Dim value As Integer
            Dim value1 As Double
            Dim headerText As String = DataGridView1.Columns(e.ColumnIndex).HeaderText
            If headerText = "Count" Then
                If (Int32.TryParse(e.FormattedValue, value)) = False Then
                    MessageBox.Show("Please enter int data!", "Erroe Message", MessageBoxButtons.YesNo, MessageBoxIcon.Error)
                    DataGridView1.CancelEdit()
                    DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 1
                End If
            ElseIf headerText = "UnitPrice" Then
                If (Double.TryParse(e.FormattedValue, value1)) = False Then
                    MessageBox.Show("Please enter decimal!", "Erroe Message", MessageBoxButtons.YesNo, MessageBoxIcon.Error)
                    DataGridView1.CancelEdit()
                    DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 1
                End If
            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.

    Wednesday, August 30, 2017 6:53 AM