tags:

views:

545

answers:

1

I have successfully set up a WPF Datagrid with the March 2009 WPF Toolkit, created LINQ-to-SQL classes from the Northwind database, bound the WPF grid with this code:

var customers = from c in _db.Customers
                select c;
TheDataGrid.ItemsSource = customers;

I can move columns from left to right, got delete columns to work, etc.

However, when I click on a column header to sort it, I get about 20 pairs of errors in my Output window, it looks as if there are a pair of errors for each column:

System.Windows.Data Error: 1 : Cannot create default converter to perform 'two-way' conversions between types 'System.Data.Linq.EntitySet`1[TestDataGrid566.Model.Order]' and 'System.String'. Consider using Converter property of Binding. BindingExpression:Path=Orders; DataItem='Customer' (HashCode=4925117); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='System.Data.Linq.EntitySet`1[TestDataGrid566.Model.Order]' BindingExpression:Path=Orders; DataItem='Customer' (HashCode=4925117); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

When I created the LINQ-to-SQL classes, I simply dragged all the tables from the database to the model designer and saved, so it the classes are all default code.

What are these errors telling me? Did I simply not set up the LINQ-to-SQL classes correctly or is this pointing to something deeper?

+3  A: 

I think you should transform your query into a list.

var customers = from c in _db.Customers
                select c;
TheDataGrid.ItemsSource = customers.ToList(); //note the .ToList() call

Otherwise, the DataGrid tries to re-enumerate the query, which is a bad idea since a query result is a lazy loaded collection.

Denis Troller
I still get the errors but at least the .ToList() allows it to sort, thanks.
Edward Tanguay
If I read the error message correctly, it is a problem in the binding. E.g, your Linq2Sql classes are probably setup fine (looks as though you have an entity 'Customer' which has a property 'Orders', which is a collection of Order entities) but you're trying to bind that collection to a - continued
Razzie
TextBlock Text property, which it can't, thus giving you those warnings. I'd look into that.
Razzie
True, either you indicated you wanted to bind to the Orders collection property, or you used the AutoGenerateColumns option (which should not be there, it's rather useless in pretty much any actual case).
Denis Troller
I tried to take autoGeneratecolumns off and define my own but I get this binding error: http://stackoverflow.com/questions/678327/how-can-i-define-my-own-columns-in-a-wpf-datagrid
Edward Tanguay
So I got AutoGenerateColumns=false to work, and now all the errors are gone, great, thanks!
Edward Tanguay