Frage

I was testing its cellValidating event which has some problem or may be i am missing something. When the radgridview cell in in edit mode i cannot click anywhere on the form thats fine here. But when i click on button, code executes and my form hangs. So my question is how can i disable code execution on button click when radgridview cell is in edit mode during cellValidating event. Note: When i debug code and insert breakpoints on cellValidating event and button. Code works well and button code does not executes. Following is the code that i am using.If anyone can immediate answer i shall be very thankful to him.

Private Sub rgv_CellValidating(sender As System.Object, e As Telerik.WinControls.UI.CellValidatingEventArgs) Handles rgv.CellValidating

        Dim column As GridViewDataColumn = TryCast(e.Column, GridViewDataColumn)
    If TypeOf e.Row Is GridViewDataRowInfo AndAlso column IsNot Nothing AndAlso column.Name = "ProductId" Then
        If String.IsNullOrEmpty(DirectCast(e.Value, String)) OrElse DirectCast(e.Value, String).Trim() = String.Empty Then
            e.Cancel = True
            DirectCast(e.Row, GridViewDataRowInfo).ErrorText = "Validation error!"
        Else
            DirectCast(e.Row, GridViewDataRowInfo).ErrorText = String.Empty
        End If
    End If
War es hilfreich?

Lösung

The phenomnenon you describe about buttoncode still executing also is true for ribbonmenu buttons and ribbonmenutabs. The hanging up problem I couldn't reproduce so I guess it has to do with the code of your executed buttons there.

For a possible workaround you could put a private variable into your form and in the cellvalidation code set it to true if an error occurs and false if no error occurs (and naturally set it fo false in the form initialization code).

Then within the buttons code you test for the variables value if its true no code is executed.

Thus if you have a ribbonform (or any other form) as class:

public partial class MF : RadRibbonForm
{
    private bool _hasValidationError;

    public MF()
    {
       ...//Normal construction handler code
       _hasValidationError = false;
    }

    void MF_CellValidating(objec sender, CellValidatingEventArgs e)
    {
         .....//Do validation and set e.Cancel true if the validation fails.
        if (e.Cancel)
        {
            hasValidationError = true;
        }
        else
        {
           hasValidationError = false;
        }
    }

    private void MyButton_Click(object sender, EventArgs e)
    {
        if (_hasValidationError)
        {
           // Before return you could put in a messagebox that there are cell errors that need to be looked at first
           return;
        }
        else
        {
            .......//Button code execution occurs here
        }
    }

With this code you can make sure that the code of the buttons you don't want to be executed won't get executed on an Cell validation error.

The Code is in C# but should work similar for VB winforms.

Andere Tipps

This seems to be a known issue with the grid issue link.

There is also work around for the issue provided:

bool validating = false;

void radGridView1_CellValidating(object sender, Telerik.WinControls.UI.CellValidatingEventArgs e)
{
    if (e.Value.ToString().Length < 5)
    {
        e.Cancel = true;
        validating = true;
        e.Row.ErrorText = "Validation error!";
    }
    else
    {
        validating = false;
    }
}

private void radButton1_Click(object sender, EventArgs e)
{
     if (!validating)
     {
         Debug.WriteLine("Executed");
     }
 }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top