0

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?

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I still get the errors but at least the .ToList() allows it to sort, thanks.
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
TextBlock Text property, which it can't, thus giving you those warnings. I'd look into that.
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).
I tried to take autoGeneratecolumns off and define my own but I get this binding error: stackoverflow.com/questions/678327/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.