Using async/await in Cell Validating event

4 posts, 1 answers
  1. CyberBotX
    CyberBotX avatar
    11 posts
    Member since:
    Nov 2016

    Posted 07 Apr 2020 Link to this post

    My issue might be similar to this one: https://www.telerik.com/forums/async-await-inside-row-validating-event

    I'm wanting to validate by cell, as opposed to by row. Similar to the above, validation works fine when no async calls are made, but progresses as if the validation wasn't there as soon as there is an async call of any sort.

    In my case, I'm wanting to use the Cell Validating event not strictly for validation, but rather to provide a prompt to the user and have them answer yes or no before progressing. But I need to be able to run the event with async/await as I am calling async methods.

    I can slightly get around this by calling the async methods with in a synchronous-like way, by calling the Wait() method on a Task, for instance. But it is much less convenient to do so.

    Is the Telerik UI for WPF library still unable to cope with async validating events like described in the above thread or is there a way to make this situation work?

    // This would be preferred
    public async void CellValidating(GridViewCellValidatingEventArgs e)
    {
        var result = await this.Dispatcher.InvokeAsync(() => MessageBox.Show("Test", "Test", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.Yes));
        if (result == MessageBoxResult.No)
        {
            e.IsValid = false;
            e.ErrorMessage = "This is an error";
        }
    }
     
    // This is what I have to do instead
    public void CellValidating(GridViewCellValidatingEventArgs e)
    {
        var op = this.Dispatcher.InvokeAsync(() => MessageBox.Show("Test", "Test", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.Yes));
        _ = op.Wait();
        var result = op.Result;
        if (result == MessageBoxResult.No)
        {
            e.IsValid = false;
            e.ErrorMessage = "This is an error";
        }
    }
  2. CyberBotX
    CyberBotX avatar
    11 posts
    Member since:
    Nov 2016

    Posted 07 Apr 2020 in reply to CyberBotX Link to this post

    I should probably add to the above that I only used calling a MessageBox via the Dispatcher as an example, I very well could load the MessageBox without invoking the Dispatcher at all. But another example would be asynchronous database calls.
  3. CyberBotX
    CyberBotX avatar
    11 posts
    Member since:
    Nov 2016

    Posted 07 Apr 2020 Link to this post

    A followup, the reason I want to be able to use an async validating method is so that the UI thread is not blocked while doing the validating. The method I described above that doesn't use async blocks the UI thread.
  4. Answer
    Martin Ivanov
    Admin
    Martin Ivanov avatar
    2582 posts

    Posted 10 Apr 2020 Link to this post

    Hello Naram,

    The statement in the forum you mentioned is still valid. I am afraid that RadGridView doesn't support this scenario.

    Regards,
    Martin Ivanov
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
Back to Top