This is a migrated thread and some comments may be shown as answers.

Cellvalidating Event and MsgBox

2 Answers 209 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Dev J
Top achievements
Rank 1
Dev J asked on 07 Dec 2009, 12:55 PM
I'm trying to pop up a MsgBox in a GridView's CellValidating event. like this:

If e.Value.ToString.Length = 0 Then 
    e.Cancel = True 
    MsgBox("Text is required.")  
End If 

The event keeps firing over and over, causing the MsgBox to appear "a million" times, and finally, the MsgBox AND the Form that contains the GridView both lose the text in their title bar.

I can use a flag like this:

If e.Value.ToString.Length = 0 Then 
    If Not validationInProgress Then 
        validationInProgress = True 
        e.Cancel = True 
        MsgBox("Text is required.")  
        validationInProgress = False 
    End If 
End If 

...and it works correctly, as long as I am trying to navigate away from the cell to some other element in the grid. If I click on any other control on the form, I get the message twice (???) but then the focus still goes to whatever other control it was I clicked on. For example, I have a "Close" button on the form, and I want the user to be able to click it to cancel their edits and not get the validation MsgBox, but instead I want to show a "Discard changes?" type dialog. Any ideas?

PS: If possible, please provide tech specs on exactly what causes the event to fire, such as the gridview losing focus, etc.

2 Answers, 1 is accepted

Sort by
0
Dev J
Top achievements
Rank 1
answered on 09 Dec 2009, 03:23 PM
Anyone?
0
Martin Vasilev
Telerik team
answered on 10 Dec 2009, 01:53 PM
Hello Dev J,

Thank you for writing. CellValidating event is raised when a cell is left no matter whether the RadGridView is in edit mode or not. This means that if you have defined a CurrentCell and you leave it (select other cell) or change the focus to another control outside the grid (for example click on a button), CellValidating event is fired.


I am not sure what exactly is the reason for the endless raising of CellValidating event, but it is possible to experience something similar if you have GridViewNewRowInfo and a condition for blank values. You can avoid that by subscribing to CellValidating event after the grid is filled up (for example in Form Load event).

You can determine when you are leaving RadGridView by using the ContainsFocus property:

Private Sub radGridView1_CellValidating(ByVal sender As Object, ByVal e As CellValidatingEventArgs)
    Debug.WriteLine([String].Format("{0}CellValidating", System.Math.Max(System.Threading.Interlocked.Increment(_num),_num - 1)))
    If Not validatingProcess AndAlso (e.Value Is Nothing OrElse e.Value.ToString() = [String].Empty) Then
        e.Cancel = True
          
        If Me.radGridView1.ContainsFocus Then
            MessageBox.Show("Validation Inside")
        Else
            MessageBox.Show("Validation Outside")
        End If
    End If
End Sub

However I confirm the described issue with the double throwing of CellValidating event if there is a pop up message in the event handler. We will investigate it further and address it for some of the next releases. I have updated your Telerik points. Unfortunately, I have not managed to find a suitable work-around for this situation. Please, excuse us for the inconvenience. 

Sincerely yours,

Martin Vasilev
the Telerik team

 


Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
GridView
Asked by
Dev J
Top achievements
Rank 1
Answers by
Dev J
Top achievements
Rank 1
Martin Vasilev
Telerik team
Share this question
or