KyloRen KyloRen - 1 year ago 229
SQL Question

Refresh WPF Datagrid not bound to observable collection?

I am using LINQ to SQL with a Datagrid and binding the data to the auto generated fields. The data updates fine when when submitting changes via LINQ.
The problem I am having is I cannot seem to refresh the updated data after I run a SQL statement on the database directly. The Databases data is updated correctly and closing down and re-starting the application the data is updated correctly.

I have tried,



private DatabaseDataContext sql = new DatabaseDataContext(


The XAML used to bind the data to a Datagrid column,
is the auto generated field from the Database table.

<DataGridTemplateColumn Header="Info" Width="*" SortMemberPath="Words">
<TextBlock FontWeight="Light" Text="{Binding Path=Info_Data, UpdateSourceTrigger=PropertyChanged}" />

By all accounts it seems that I need to create an observable collection and bind the data to that and update the collection to refresh the data-grid table.
Is there any way around this?

Answer Source

OK, I have found out what I was doing so that the DataGrid was not updating.

I was not disposing of the DataContext after each use. To keep with the SQL to LINQ, Refreshing of the data was achieved by creating a new DataContext for each time I was updating the DataBase.

The below code automatically disposes of the DataContext when finished and then when making the new DataContext the updated data shows in the DataGrid

using (DatabaseDataContext sql = new DatabaseDataContext(ConnectionString)) 
       //Your code
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download