views:

51

answers:

3

I have a query that hits a service and returns the results back to me as an indexed collection

static void QueryActivities()
{
    QueryClient qc = new QueryClient("BasicHttpBinding_IQuery");
    QueryFilter filter = new QueryFilter();
    filter.CallForService = false;
    var result = qc.GetFilteredActivityIndex(filter);
    result.Clone(); //inserted this just to test return in debug mode
}

WHen this is executed I get a return of 5 records ( actually five rows of data with multiple values per each row. id, type, date, address etc).

I would like to bind this return to a gridview within a WPF application. Is there a way to refernce and bind to result variable or do I need to create a new collection and then add the return of result to the new collection?

thank you

A: 

There might be a way with WPF Toolkit's DataGrid. However, I would create a custom object and make an ObservableCollection of that object and bind to it. The ObservableCollection is a collection meant for this type of use and you can bind the elements inside your custom object to the different elements in your gridview.

Update:

Based on your comment it feels as if you need a good tutorial for binding with MVVM. The best tutorial I have seen on this is Jason Dolinger's tutorial. It helps you bind associated properties from a collection of a custom class.

jsmith
A: 

A new class(ViewModel) level Collection with its associated property would be better.

Veer
could you provide a snippet of sample code for this? When you state associated properties are you referring to setting up a dependency property on the view? I'm good with the binding part (control to collection) but not the part on binding the return to a new collection. This is somehting I thinkk would really be useful to learn. In the above snippet I haven't been able to pass the result to new collection.Thank you!
randyc
@randyc: What's the type of `result`?
Veer
Result is a enumerated list being returned.[0] .....[1] .......
randyc
@randyc: In case you're still struggling... `result = new ObservableCollection(qc.GetFilteredActivityIndex(filter));` where result is a property of type `ObservableCollection`. you can now bind this result to your control. We use ObservableCollection to notify changes when property or collection itself is changed.
Veer
Got it working but thanks on the tip for creating the Collection
randyc
A: 

After some testing I found that I could bind directly to the query. The variabl;e results acts as it's own List Collection. That being said I have been able to display the data using:

    private void QueryActivities()
    {
        QueryClient qc = new QueryClient("BasicHttpBinding_IQuery");
        QueryFilter filter = new QueryFilter();
        filter.CallForService = false;
        var result = qc.GetFilteredActivityIndex(filter);

        this.actGridView.ItemsSource = result; //binds to the gridview
    }

Thanks for all the comments on using collections.

randyc