OrPaz
OrPaz

Reputation: 1085

WPF Datagrid binding to ObservableCollection no columns generated

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!!

Upvotes: 0

Views: 993

Answers (1)

Alex Paven
Alex Paven

Reputation: 5549

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.

Upvotes: 1

Related Questions