locked
help with validating grids RRS feed

  • Question

  • I have a DataGridView with two bound columns. I want to validate that the user enters a valid number in one of the columns. I managed to handle the cell validating event to check for a valid number. Here is the code:

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

            Dim row As DataGridViewRow = m_dtgTasas.Rows(e.RowIndex)
            Dim cellTasa As DataGridViewCell = row.Cells("Tasa")
            Dim tasa As Double

            cellTasa.ErrorText = ""
            e.Cancel = False

            If m_dtgTasas.Rows(e.RowIndex).IsNewRow Then Return

            If (m_dtgTasas.Columns(e.ColumnIndex).Name = "Tasa") Then
                If Not Double.TryParse(cellTasa.EditedFormattedValue.ToString(), tasa) Then
                    e.Cancel = True
                    cellTasa.ErrorText = "Some Error Text"
                End If
            End If
        End Sub

    but with this code, if I edit the column to an invalid value, and then press the Esc key, it keeps trying to validate the cell and it doesn't change the cell to its original value... How can I make that to work?

    Thanks..



    Tuesday, April 22, 2008 11:23 PM

Answers

  •  

    Hi rassiel,

    I performed a testing according to your code, but I didn’t reproduce the problem. Here is my testing code.

    Code Snippet

    Public Class Form1

     

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            Dim dt As New DataTable()

            dt.Columns.Add("Pid")

            dt.Columns.Add("price")

            For i As Integer = 0 To 10

                dt.Rows.Add("product" & i, i + CSng(i) / 2.0)

            Next

            dt.AcceptChanges()

            Me.DataGridView1.DataSource = dt

        End Sub

     

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

            If DataGridView1.Rows(e.RowIndex).IsNewRow Then Return

            If e.FormattedValue Is Nothing Then Return

            Me.DataGridView1.CurrentCell.ErrorText = ""

     

            Dim enteredValu As Double

            If (DataGridView1.Columns(e.ColumnIndex).Name = "price") Then

                If Not Double.TryParse(e.FormattedValue, enteredValu) Then

                    e.Cancel = True

                    Me.DataGridView1.CurrentCell.ErrorText = "Some Error Text"

                End If

            End If

     

        End Sub

    End Class

     

    Based on my experience, the reason why the cell cannot get its original value is that you have commit the changes before the validation is actually completed. Most likely you have call the DataGridView.CommitEdit method and accept the changes for the underlying data list if the list implements IEditableObject interface before the CellValidating event.

    Let me know if this helps. If not, could you please provide more detailed information? Some more code would be helpful.

    Best regards.
    Rong-Chun Zhang

    Monday, April 28, 2008 8:03 AM

All replies

  •  

    Hi rassiel,

    I performed a testing according to your code, but I didn’t reproduce the problem. Here is my testing code.

    Code Snippet

    Public Class Form1

     

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            Dim dt As New DataTable()

            dt.Columns.Add("Pid")

            dt.Columns.Add("price")

            For i As Integer = 0 To 10

                dt.Rows.Add("product" & i, i + CSng(i) / 2.0)

            Next

            dt.AcceptChanges()

            Me.DataGridView1.DataSource = dt

        End Sub

     

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

            If DataGridView1.Rows(e.RowIndex).IsNewRow Then Return

            If e.FormattedValue Is Nothing Then Return

            Me.DataGridView1.CurrentCell.ErrorText = ""

     

            Dim enteredValu As Double

            If (DataGridView1.Columns(e.ColumnIndex).Name = "price") Then

                If Not Double.TryParse(e.FormattedValue, enteredValu) Then

                    e.Cancel = True

                    Me.DataGridView1.CurrentCell.ErrorText = "Some Error Text"

                End If

            End If

     

        End Sub

    End Class

     

    Based on my experience, the reason why the cell cannot get its original value is that you have commit the changes before the validation is actually completed. Most likely you have call the DataGridView.CommitEdit method and accept the changes for the underlying data list if the list implements IEditableObject interface before the CellValidating event.

    Let me know if this helps. If not, could you please provide more detailed information? Some more code would be helpful.

    Best regards.
    Rong-Chun Zhang

    Monday, April 28, 2008 8:03 AM
  • thanks, I couldn't reproduce the error neither, I created some other form with the same logic and they worked fine. Thanks for your answer.
    Tuesday, April 29, 2008 4:19 PM