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

Using async/await in Cell Validating event

3 Answers 359 Views
GridView
This is a migrated thread and some comments may be shown as answers.
CyberBotX
Top achievements
Rank 2
Veteran
CyberBotX asked on 07 Apr 2020, 08:51 PM

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";
    }
}

3 Answers, 1 is accepted

Sort by
0
CyberBotX
Top achievements
Rank 2
Veteran
answered on 07 Apr 2020, 09:25 PM
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.
0
CyberBotX
Top achievements
Rank 2
Veteran
answered on 07 Apr 2020, 10:01 PM
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.
0
Accepted
Martin Ivanov
Telerik team
answered on 10 Apr 2020, 11:52 AM

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.
Tags
GridView
Asked by
CyberBotX
Top achievements
Rank 2
Veteran
Answers by
CyberBotX
Top achievements
Rank 2
Veteran
Martin Ivanov
Telerik team
Share this question
or