views:

38

answers:

1

Hello,

I am binding a datagrid to some data and using the AutoColumnGeneration. When using a regular linq query against a dataset, everything works fine :

var queryAll = from actor in actorsAll
select new
   {
       ActorName = actor.IsPerson ? actor.FirstName + " " + actor.LastName : actor.CompanyName
   };

    MalatDetailsBudgetGridUC.ItemsSource = queryAll;

But as i want my grid to be binded to an ObservableCollection, i am trying to use the followings:

ActorsCollection collection = new ActorsCollection(actorType);
var queryAll = from actor in collection
    select new
    {
        ActorName = actor.IsPerson ? actor.FirstName + " " + actor.LastName : actor.CompanyName
    };

MalatDetailsBudgetGridUC.ItemsSource = queryAll;

When using this, my grid get populated with (tiny thin) rows exactly as it should be, but no columns are generated.

B.T.W - ActorsCollection is an implemented ObservableCollection that adds itself with the Actor entities.

Please Help!!

+1  A: 

Well, can't say I understand much: the title says datagrid, but in the question you talk about treeview.

There shouldn't be any difference between the two code blocks, assuming you set the queryAll as the DataContext of the grid. Are you sure the DataContext is set/bound correctly?

edit: also try looking with the debugger at the types created by the anonymous constructors, that might give you some ideas.

edit: to add the answer here, the problem was that the second form creates an iterator and the datagrid (interestingly) gets confused by it. The quickest way is to force the enumerator to generate the collection with ToList() or similar.

Alex Paven
Sorry about the tree/grid mistake. I have just edited it. It is all about a DataGrid.About the types created : When using the dataset(block 1) i get an ObjectQuery created, but when using the ObservableCollection i get the following type created : System.Linq.Enumerable.WhereSelectEnumerableIteratorAnd the second block ofcourse does not work the same as the first as i already mentioned.
OrPaz
Try forcing ToList() on the iterator then and use that instead.
Alex Paven
PERFECT! using ToList() has solved my issue. Thank you!!
OrPaz