Linq + WPF DataGrid

Jun 7, 2009 at 10:08 AM

Hi Guys,

I'm just getting started with .net in general really...

I am making what i think is a fairly simple app. I have a database with a couple of tables and some datagrids which i want to display a combination of data from the data tables.

I have the linq queries displaying in the datagrids but i'm a bit stumped as to how to write back to the database, and when i do (by searching the database for the item id and updateing the data in the table) the wpf datagrid bound to the data does not update even though i thought linq implemented iNotifyPropertyChanged.


Whats the best/simplest way to bind a linq query (and not just a straight table dump i.e. two tables with reference between then) to a datagrid control? And how do i write data back to the database if a user clicks a button or adds a new line to the datagrid?


Currently i have a Class VisitorsTable which just gets the linq datacontext and has a function which returns a Table<CurrentVisitor> call getVisitors;

Then in my App.xml i declare an ObjectDataProvider which uses MethodName getVisitors.

and in my datagrid i use ItemSource="{Binding Source={StaticResource VisitorsDataProvider}}"


Any help would be greatly appreciated.

C# 3.5 + Linq + WPF is great but it can be a little tricky getting my head around how it all works. :)

Jun 12, 2009 at 12:51 PM

To write back to the database, use dataContext.SubmitChanges().  Some places that are common to do this is when a row is committed or a new item is added.  You can listen to the RowEditEndingEvent and submit changes there.

Jun 13, 2009 at 10:42 AM

Thanks vinsibal.

I made a RowEditEndingEvent and put a dc.SubmitChanges() call in. If i edit a row nothing happens (but the event is called), which is wierd.

If i put in a new row an unhandeled exception is thrown but this because i proberbly don't have a handler.


One clue as to why it isn't happening might be this.

I'm using AutoGenerColumns but in my debug output i'm getting

"System.Windows.Data Error: 39: BindingExpression path error: '*COLUMNNAME*' property not found on 'object' element is 'TextBlock' (Name=''); target property is 'Text' (type 'String)


Any idea's how to fix that?

all i'm doing to generate the data is creating a datacontext to the linq data in my class initalizing it and then assigning the datacontext of the grid to it.

The current data is displayed in the grid, but if i edit nothing :(