Bill Dickinson

Bill Dickinson

  • NA
  • 1
  • 1.7k

Initializing WPF Datagrid bound to ling to sql dbml

Nov 18 2013 11:13 PM
I have a case in which data external to a database is displayed in a WPF DataGrid.  Currently, the data comes in as an ObservableCollection of type T, where each T is a class that models a database table.

Some of the data may already exist in the database table, but some may be new and will not be stored in the database.  The DataGrid provides a way of editing/adding new items to the table and viewing those items that have already been analyzed (they exist in the database).

So, here's my problem - I would like to use Linq to SQL to create a dbml datacontext on the table for CRUD operations.  However, the data coming in when the grid is loaded is not from the database.  I check the database for items that match what's in the ObservableCollection and highlight them in the grid to indicate that they already exist, and to highlight those items in the ObservableCollection that are not in the database.

My first inclination is to bind the DataGrid to the ObservableCollection on the load operation, and then switch the context to the dbml datacontext after the grid is loaded.  From that point forward, I would expect the CRUD operations to make their way to the database.

Is this a reasonable approach?  I bet there's something better and I would appreciate feedback on how best to tackle this problem.  Thanks.