Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Making DataGridView DataError set cell error text/icon
 

Making DataGridView DataError set cell error text/icon

Hi ,

I have an integer column in DataGridView and i dont want to CancelEdit when the user enter non numirical value, insted i need to force error icon to apear after 

e.ThrowException = False

how can i do it??

My code is:

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

        If DataGridView1.Columns(e.ColumnIndex).Name = "NumiricText1" Then
                       Me.DataGridView1.Rows(e.RowIndex).ErrorText = ""
            Dim newInteger As Integer

             If DataGridView1.Rows(e.RowIndex).IsNewRow Then Return
            If Not Integer.TryParse(e.FormattedValue.ToString(), newInteger) _
                OrElse newInteger < 0 Then
                e.Cancel = True
                Me.DataGridView1.Rows(e.RowIndex).ErrorText = "the value must be a non-negative integer"
            End If

        End If

    End Sub

Private Sub DataGridView1_DataError(ByVal sender As Object, _
 ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) _
 Handles DataGridView1.DataError

        e.ThrowException = False

End Sub

 

Ehaboss  Friday, October 02, 2009 2:11 PM

Hi Ehaboss,

Based on my understanding, the issue is that when you move your mouse to one cell, you can only see the recent error text. To solve this issue, we can store the error information in the Tag property of the cells and handle the CellMouseEnter event to set the error text and handle the CellMouseLeave event to clear the error text. The code snippet below shows my ideas:

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

        If DataGridView1.Columns(e.ColumnIndex).Name = "NumiricText1" Then
            Me.DataGridView1(e.ColumnIndex, e.RowIndex).Tag = ""
            Dim newInteger As Integer

            If DataGridView1.Rows(e.RowIndex).IsNewRow Then Return
            If Not Integer.TryParse(e.FormattedValue.ToString(), newInteger) _
                OrElse newInteger < 0 Then
                e.Cancel = True
                'Save the error text in Tag property.
                Me.DataGridView1(e.ColumnIndex, e.RowIndex).Tag = "the value must be a non-negative integer"
            End If
        End If
    End Sub

    Private Sub DataGridView1_DataError(ByVal sender As Object, _
     ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) _
     Handles DataGridView1.DataError
        e.ThrowException = False
    End Sub

    Private Sub DataGridView1_CellMouseEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellMouseEnter
        Dim cell As DataGridViewCell = Me.DataGridView1(e.ColumnIndex, e.RowIndex)
        If ((Not (cell.Tag Is Nothing)) Or cell.Tag.ToString().Trim() <> "") Then
            'Set the error text.
            Me.DataGridView1.Rows(e.RowIndex).ErrorText = cell.Tag.ToString()
        Else
            Me.DataGridView1.Rows(e.RowIndex).ErrorText = ""
        End If
    End Sub

    Private Sub DataGridView1_CellMouseLeave(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellMouseLeave
        Me.DataGridView1.Rows(e.RowIndex).ErrorText = ""
    End Sub

I did not test the code, so please let me know if it does not work.

Regards,
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, October 07, 2009 5:42 AM
Have you tried setting the ErrorText to something other than an empty string?
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Friday, October 02, 2009 2:31 PM
Sure, you can see it in the code example i already posted :
Me.DataGridView1.Rows(e.RowIndex).ErrorText = "the value must be a non-negative integer"
Ehaboss  Friday, October 02, 2009 4:02 PM
Hi ,

I have an integer column in DataGridView and i dont want to CancelEdit when the user enter non numirical value, insted i need to force error icon to apear after 

e.ThrowException = False

how can i do it??

My code is:

 Private Sub DataGridView1_CellValidating(ByVal sender As Object,  _ 

ByVal e As DataGridViewCellValidatingEventArgs)  _ 

Handles DataGridView1.CellValidating



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

------------------> HERE    Me.DataGridView1.Rows(e.RowIndex).ErrorText = ""

            Dim newInteger As Integer



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

            If Not Integer.TryParse(e.FormattedValue.ToString(), newInteger) _

                OrElse newInteger < 0 Then

                e.Cancel = True

                Me.DataGridView1.Rows(e.RowIndex).ErrorText = "the value must be a non-negative integer"

            End If



        End If



    End Sub



Private Sub DataGridView1_DataError(ByVal sender As Object, _

 ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) _

 Handles DataGridView1.DataError



        e.ThrowException = False



End Sub

 


No ... I meant currently where you are setting it to an empty value. Isn't that where you want it to work?


www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Monday, October 05, 2009 5:47 AM
No, i set Error string to none empty value only if the string intered is not integere >0 but i always initialize it with empty string so if the input string is good data, the error string will be empty.  
Ehaboss  Monday, October 05, 2009 6:28 AM

Hi Ehaboss,

Based on my understanding, the issue is that when you move your mouse to one cell, you can only see the recent error text. To solve this issue, we can store the error information in the Tag property of the cells and handle the CellMouseEnter event to set the error text and handle the CellMouseLeave event to clear the error text. The code snippet below shows my ideas:

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

        If DataGridView1.Columns(e.ColumnIndex).Name = "NumiricText1" Then
            Me.DataGridView1(e.ColumnIndex, e.RowIndex).Tag = ""
            Dim newInteger As Integer

            If DataGridView1.Rows(e.RowIndex).IsNewRow Then Return
            If Not Integer.TryParse(e.FormattedValue.ToString(), newInteger) _
                OrElse newInteger < 0 Then
                e.Cancel = True
                'Save the error text in Tag property.
                Me.DataGridView1(e.ColumnIndex, e.RowIndex).Tag = "the value must be a non-negative integer"
            End If
        End If
    End Sub

    Private Sub DataGridView1_DataError(ByVal sender As Object, _
     ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) _
     Handles DataGridView1.DataError
        e.ThrowException = False
    End Sub

    Private Sub DataGridView1_CellMouseEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellMouseEnter
        Dim cell As DataGridViewCell = Me.DataGridView1(e.ColumnIndex, e.RowIndex)
        If ((Not (cell.Tag Is Nothing)) Or cell.Tag.ToString().Trim() <> "") Then
            'Set the error text.
            Me.DataGridView1.Rows(e.RowIndex).ErrorText = cell.Tag.ToString()
        Else
            Me.DataGridView1.Rows(e.RowIndex).ErrorText = ""
        End If
    End Sub

    Private Sub DataGridView1_CellMouseLeave(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellMouseLeave
        Me.DataGridView1.Rows(e.RowIndex).ErrorText = ""
    End Sub

I did not test the code, so please let me know if it does not work.

Regards,
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, October 07, 2009 5:42 AM

You can use google to search for other answers

Custom Search

More Threads

• How to Unselect item of ListBox?
• find the RowIndex in the DragEnter event of the DataGridView.
• Accessing DataTable rows at design time
• Lookup where is the primary key of a foreign key
• Problem with Databound DataGrid on a MDI Child form
• combo box - how to prevent selectedindex change
• Combining Info from Two List Boxes
• how can i select all checkboxes by clicking a button or checking another checkbox
• Checkbox in DatagridView Win Form
• Export to Excel Template