如何刷新 WPF DataGrid?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/930350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:29:35  来源:igfitidea点击:

How to refresh a WPF DataGrid?

wpfdata-bindingdatagrid

提问by KovBal

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.

我有一个带有一些数据的 WPF DataGrid。您可以通过单独的窗口添加行。DataContext 是相同的,一个 LINQ-to-SQL 对象。绑定也是一样,我将“ItemsSource”属性绑定到一个表。

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

在另一个窗口中,当用户单击“保存”时,我以编程方式创建一行并使用“InsertOnSubmit”添加它。之后,我使用 DataContext 的“SubmitChanges”方法。

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.

我的问题是 DataGrid 没有更新。如果我重新启动应用程序,我可以看到新行,所以它在数据库中,但我找不到刷新 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?

到目前为止,我已经尝试在 DataGrid 的 BindingExpression 上使用“UpdateTarget”,但没有帮助。我也试过“dataGrid.Items.Refresh()”——同样的结果。我怎样才能解决这个问题?

采纳答案by Ana Betts

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.

它不更新的原因是 LINQ-to-SQL 没有实现 INotifyCollectionChanged,因此 WPF 无法判断 ItemsSource 已更新。解决此问题的最不可怕的方法是将您的 LINQ-to-SQL 结果复制到 ObservableCollection - 当您执行 Insert 时,还要添加到 observable 集合。然后你会看到更新。

回答by nullpointer

回答by Dark Daskin

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:

我遇到了同样的问题,发现 ObservableCollection 的最佳位置是 DataContext。它有一些设计器生成的部分方法,可用于更新集合。这段代码工作得很好:

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);
    }
}

回答by Stephan

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.

问题是您需要刷新 LINQ-to-SQL DataContext。即使在提交更改后,DataContext 也无法正确识别新行。您需要处理您拥有的 DataContext 并创建一个新的。在大多数情况下,DataContext 应该用于一个短期操作,而不是作为一个长期存在的对象。

回答by user504737

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.

如果您遇到必须在另一个窗口中重新加载网格的情况,您可以简单地关闭该窗口并再次调用它。

回答by Orson

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

由于某种原因Items.Refresh()对我不起作用。有效的是让我的底层集合继承ObservableCollection然后调用它的Add方法。

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

ContactUIObjectsis just the grids underlying collection.

ContactUIObjects只是网格底层集合。

回答by Branimir

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

或者只是再次调用搜索代码(通常是搜索按钮)> 我已经解决了这个问题。