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

[Solved] Catch 22 with async web service

2 Answers 106 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Steven
Top achievements
Rank 1
Steven asked on 20 Aug 2011, 10:09 AM
I have a GridView with a ComboBox column whereby the combobox is bound to a web service result. The problem is that I can't find the right event to perform the ItemsSource assignment. If I put the assignment in the UserControl_Loaded the web service call hasn't completed so the results are empty. If I put the assignment in the Completed event then the combobox control is not instantiated. What is the proper sequence for using a combobox that is in a grid which is populated from a web service? An example would help a lot.

2 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 22 Aug 2011, 10:02 AM
Hi Steven,

You can try a radically different approach.

You can instantiate a totally empty observable collection and bind the combo's ItemsSource to it. Then once the data arrives on the client, you can add the data to this observable collection and hopefully the combo will fill up thanks to the fact that it is an observable collection and it supports INotifyCollectionChanged.

Maybe this approach would help.

Kind regards,
Ross
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Steven
Top achievements
Rank 1
answered on 22 Aug 2011, 04:04 PM

I actually did something similar.   I created an async handler class derived from a list class.   Basically, I did an Add operation for each async method call that needed to be loaded before the main load occurs.   Then, as each method completes, it decrements the count and when the count is back to zero, then everything is loaded and a completed event is fired.   Here is the class:

public class ESWebCallsHelper : List<ESWebBase>
{
    private int _loadingCount;
    public delegate void OnLoadedHandler();
    public event OnLoadedHandler OnLoaded;
    public ESWebCallsHelper()
    {
        _loadingCount = 0;
    }
    public new ESWebBase Add(ESWebBase model)
    {
        base.Add(model);
        model.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Model_PropertyChanged);
        _loadingCount++;
        return model;
    }
    void Model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        _loadingCount--;
        if (_loadingCount == 0)
        {
            OnLoaded();
        }
    }
    public bool IsLoading()
    {
        return (_loadingCount > 0);
    }

And then to use it:

public partial class MainPage2 : UserControl
{
    private EmployeeList empList;
    private Positions positionList;
    private Location locationList;
    public MainPage2()
    {
        ESWebCallsHelper batchCalls = new ESWebCallsHelper();
        batchCalls.OnLoaded += new ESWebCallsHelper.OnLoadedHandler(batchCalls_OnLoaded);
        empList = (EmployeeList)batchCalls.Add(new ViewModels.EmployeeList());
        positionList = (Positions)batchCalls.Add(new ViewModels.Positions());
        locationList = (Location)batchCalls.Add(new ViewModels.Location());
    }
    void batchCalls_OnLoaded()
    {
        InitializeComponent();
    }

This seemed to work very well.    Now, within the batchCalls_OnLoaded, I could do the ItemsSource assignments because everything was ready and available.   Worked nicely.
Tags
GridView
Asked by
Steven
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Steven
Top achievements
Rank 1
Share this question
or