0

I have to bind datagrid in silverlight.

void proxy_DoWorkCompleted(object sender, ServiceReference1.DoWorkCompletedEventArgs e)
    {


        try
        {
            //var v = e.Result as Queryable;
            //PagedCollectionView pagesEmployees = new PagedCollectionView(v);
            //dpGridPager.Source = pagesEmployees;
            dataGrid1.ItemsSource = e.Result;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.InnerException.ToString());
        }
    }

Now this e.Result should be IEnumerable to bind datagrid. How Should i Convert this to IEnumerable??

Anders Abel
  • 67,989
  • 17
  • 150
  • 217
Mohan
  • 907
  • 5
  • 22
  • 45
  • Please show the service contract. How would we know what e.Result should be without that? ` – John Saunders Nov 26 '11 at 19:13
  • @John Saunders : thank you for you reply. I'm returning datatable from my service and want to convert it to Ienumerable. – Mohan Nov 28 '11 at 10:53

2 Answers2

5

e.Result contains the result of the DoWork operation; if the return type of the service operation is enumerable, then e.Result will also be. So update the service operation to return something which is enumerable, then update the service reference.

carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
  • Thanks for your reply. Suppose I'm returning datatable from service then?? – Mohan Nov 28 '11 at 07:23
  • I'm agreed with you. but what if my service returns data table. – Mohan Nov 28 '11 at 10:47
  • The `DataTable` type doesn't exist in Silverlight, so you'll probably get something like `ArrayOfXElement` as the type of e.Result which contains the XML of the response. If it works for you (i.e., in SL you'll parse the XML yourself, then convert it to enumerable), great, otherwise you should use some data type as the return value of your operation. – carlosfigueira Nov 28 '11 at 14:24
0

Sometimes it is useful to use a single object as the data source to a control that expects an IEnumerable as data source. I use a small extension method to handle such cases:

public static class ObjectExtensions
{
    public static IEnumerable<T> WrapInEnumerable<T>(this T t)
    {
        yield return t;
    }
}

Now if you have an object returned from your DoWork method, you can databind by using:

myControl.DataSource = DoWork().WrapInEnumerable();
Anders Abel
  • 67,989
  • 17
  • 150
  • 217