Datagridview Validation RRS feed

  • Question

  • I have an unbound Datagridview, I need to validate contents of cells when the user attempts to change cells (i.e. when they click a new cell or tab but before they enter the new cell).  I cannot do this in Cellvalidating as (I think) this is fired whenever the cell contents change, I can't do it there because the cell contents must be complete to validate (for example some cells contain person's names).


    Monday, January 29, 2018 6:04 PM

All replies

  • Here is where having a bound DataGridView you could do validation in events for the data rather than the control. For instance if you had used a DataTable you could subscribe to DataColumChanging (see simple example)

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, January 29, 2018 6:58 PM
  • Hi lb51,

    Maybe you can try to use DataGridView1_CellValueChanged event, the following code verifies that the value of second column is double or not.

    Private Sub FrmDataGridview_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.ColumnCount = 3
            DataGridView1.Columns(0).Name = "Product ID"
            DataGridView1.Columns(1).Name = "Product Name"
            DataGridView1.Columns(2).Name = "Product_Price"
            Dim row As String() = New String() {"1", "Product 1", "1000"}
            row = New String() {"2", "Product 2", "2000"}
            row = New String() {"3", "Product 3", "3000"}
            row = New String() {"4", "Product 4", "4000"}
        End Sub
        Dim price As Double
        Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
            Dim value As Boolean = Double.TryParse(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value, price)
            If e.ColumnIndex = 2 Then
                If value = True Then
                    DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 0
                    MessageBox.Show("Please enter the correct value format")
                End If
            End If
        End Sub

    Best Regards,


    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

    Tuesday, January 30, 2018 5:49 AM

  • With this addition the code from Cherry does what is asked with Option Strict On

            Dim value As Boolean = Double.TryParse(CStr(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value), price)

    If she changes it herself, I can remove this message. 

    Success Cor

    Tuesday, January 30, 2018 8:18 AM