0

This is my code . I am simply trying to select all students in my Students table ( which has 5 records, all visible in the Server Explorer )

var students = from p in schoolEntities.students
               select p;

               myGrid.DataContext = students.ToList();

This is my XAML for datagrid

<DataGrid ItemsSource="{Binding}" x:Name="myGrid  /> // alignment etc left out 

But this is what I am getting

enter image description here

I'm sure it is something trivial that I am leaving out above because binding the same DataGrid to , say a list of a class Person works fine, but using Linq for my database isn't working.

UPDATE: ( I hope below is clear) I just discovered that the Students table that has two columns Id and Name has a many to many relationship with Subjects table , which has id and SubjectName and there is a StudentsSubjects table with two columns StudentId, SubjectId which makes the many to many relationship work.

That is the cause of the problem. If I bind this datagrid to a simple Teacherstable it works because teachers table has no relationships.

Now I understand the problem, but I still don't know how to fix it :(

Thank you

iAteABug_And_iLiked_it
  • 3,725
  • 9
  • 36
  • 75
  • DataContext takes an IEnumerable, so ToList is actually not necessary, but may not be the issue here – Samuel Jul 10 '13 at 12:27
  • @Samuel not using Tolist throws "Direct binding to query is not supported exception" The problem is being caused by the Many to Many relationship of Students table... still I don't know how to fix that :( – iAteABug_And_iLiked_it Jul 10 '13 at 12:33
  • Furthermore it looks ok, try a manual IEnumerable object for the grid as I do not see any issues. For broken Bindings you can check the logs: http://stackoverflow.com/questions/337023/how-to-detect-broken-wpf-data-binding – Samuel Jul 10 '13 at 12:35
  • I see, if such many to many relation creates a circular reference then it should be an issue to display it here. You probably want to create a custom data template for Person that handles the reference to other Persons gracefully – Samuel Jul 10 '13 at 12:36
  • OK, I managed to fix it by doing a join ( duh to myself) with this linq to sql join query 'var students = from t1 in schoolEntities.Students join t2 in schoolEntities.Subjects on t1.Id equals t2.Id select t1; myGrid.ItemsSource = students.ToList();' – iAteABug_And_iLiked_it Jul 10 '13 at 12:48

0 Answers0