KovBal
KovBal

Reputation: 2177

How to refresh a WPF DataGrid?

I have a WPF DataGrid with some data. You can add rows through a separate window. The DataContext is the same, a LINQ-to-SQL object. Binding is also the same, I bind the ItemsSource property to a table.

In the other window, when the user clicks on Save, I create a row programmatically and add it using InsertOnSubmit. After that I use the DataContext's SubmitChanges method.

My problem is that the DataGrid isn't updated. If I restart the application I can see the new row, so it's in the database, but I couldn't find a way to refresh the DataGrid.

So far I've tried to use UpdateTarget on the BindingExpression of the DataGrid, but it didn't help. I've also tried dataGrid.Items.Refresh() — same result. How can I fix this?

Upvotes: 29

Views: 91685

Answers (7)

Ana Betts
Ana Betts

Reputation: 74654

The reason it's not updating is that LINQ-to-SQL doesn't implement INotifyCollectionChanged, so WPF has no way to tell that the ItemsSource has been updated. The least terrifying way to fix this, is to copy your LINQ-to-SQL results to an ObservableCollection - when you do the Insert, also add to the observable collection. Then you'll see the update.

Upvotes: 21

Orson
Orson

Reputation: 75

For some reason Items.Refresh() is not working for me. What did work was to make my underlying collection inherit ObservableCollection and then call its Add method.

((ContactUIObjects)dgrdContacts.ItemsSource).Add(new ContactUIObject(o));

ContactUIObjects is just the grids underlying collection.

Upvotes: 0

Dark Daskin
Dark Daskin

Reputation: 1474

I ran into same problem and found that best place for ObservableCollection is DataContext. It has some partial methods generated by designer that can be used to update collection. This code works pretty well:

partial class DataClassesDataContext
{
    private ObservableCollection<Task> taskCollection;
    public ReadOnlyObservableCollection<Task> TaskView { get; private set; }

    partial void OnCreated()
    {
        taskCollection = new ObservableCollection<Task>(Tasks);
        TaskView = new ReadOnlyObservableCollection<Task>(taskCollection);
    }

    partial void InsertTask(Task instance)
    {
        taskCollection.Add(instance);
        this.ExecuteDynamicInsert(instance);
    }

    partial void DeleteTask(Task instance)
    {
        taskCollection.Remove(instance);
        this.ExecuteDynamicDelete(instance);
    }
}

Upvotes: 4

nullpointer
nullpointer

Reputation: 694

try datagrid.Items.Refresh() from here http://programmer.wrighton.org/2009/01/wpf-datagrid-items-refresh.html

Upvotes: 67

user504737
user504737

Reputation: 11

If you have a case when you have to reload a grid in another window , you can simply close that window and invoke it again.

Upvotes: 1

Branimir
Branimir

Reputation: 53

Or just invoke the search code again (usually the search button)> I have solved it in my case like this.

Upvotes: 0

Stephan
Stephan

Reputation: 5488

The problem is that you need to refresh your LINQ-to-SQL DataContext. The DataContext's won't properly recognize the new row even after a submit changes. You need to dispose the DataContext you have and create a new one. In most cases DataContext should be used for one short operation and not as a long standing object.

Upvotes: 2

Related Questions