如何在 C#/WPF 中刷新 DataGrid?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19905450/
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-13 10:02:09  来源:igfitidea点击:

How to refresh DataGrid in C# / WPF?

c#wpfxamldatagridrefresh

提问by Ben Clarke

private void cmbEmployee_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    string employee = (e.AddedItems[0] as ComboBoxItem).Content as string;

    dgFake.ItemsSource = newdal2.SelectUser(employee).Tables[0].DefaultView;
}

This method populates the data grid on my WPF windows form by a specific employee when this employee is clicked from a combo box, however, when I click another employee after the first, it doesn't refresh the data grid, but instead adds that employee's data beneath the first.

当从组合框中单击该员工时,此方法由特定员工在我的 WPF 窗口表单上填充数据网格,但是,当我在第一个员工之后单击另一名员工时,它不会刷新数据网格,而是添加该员工的数据低于第一个。

How do you refresh or delete the items in a Data Grid baring in mind this is on WPF Xaml Windows Forms, and it is not a DataGridView. I have tried these already and none have worked:

你如何刷新或删除数据网格中的项目,记住这是在 WPF Xaml Windows 窗体上,而不是 DataGridView。我已经尝试过这些,但都没有奏效:

dgFake.Items.Refresh();
dgFake.Items.Remove(); //Required a remove item passed to the method, so too specific
dgFake.Itemssource = "";

回答by Sheridan

In general using WPF, we manipulate data, not UI elements. Therefore, after Bindinga collection property to the DataGrid.ItemsSourceproperty, we can simply work with the collection property:

通常使用 WPF,我们操作数据,而不是UI 元素。因此,在Binding对属性进行集合属性之后DataGrid.ItemsSource,我们可以简单地使用集合属性:

In XAML:

在 XAML 中:

<DataGrid ItemsSource="{Binding YourCollection}" ... />

Then in code:

然后在代码中:

YourCollection.Clear();

Or to change items:

或更改项目:

YourCollection = someNewCollection;

You will need to implement the INotifyPropertyChangedinterfacein order for the DataGridto update automatically after changing the data like this.

您将需要实现该INotifyPropertyChanged接口,以便DataGrid在像这样更改数据后自动更新。



UPDATE >>>

更新 >>>

In response to the comment: 'I have added the XAML code, when you talk about 'YourCollection' in the XAML code what needs to be put here?':

回应评论:“我已经添加了 XAML 代码,当你在 XAML 代码中谈论‘YourCollection’时,需要在这里放什么?”:

You'll need to create a Bindablecollection property in your code; this can either be a DependencyPropertyin your code behind, or a CLR property that implements the INotifyPropertyChangedinterface. We generally don't display database elements in the UI, instead preferring to define object classes with the required properties:

您需要Bindable在代码中创建一个集合属性;这可以是DependencyProperty后面代码中的 ,也可以是实现INotifyPropertyChanged接口的 CLR 属性。我们通常不会在 UI 中显示数据库元素,而是更喜欢定义具有所需属性的对象类:

public static DependencyProperty EmployeesProperty = DependencyProperty.Register(
    "Employees", typeof(ObservableCollection<Employee>), typeof(YourUserControl));

public ObservableCollection<Employee> Employees
{
    get { return (ObservableCollection<Employee>)GetValue(EmployeesProperty); }
    set { SetValue(EmployeesProperty, value); }
}

Then in your cmbEmployee_SelectionChangedhandler method, you can update the collection property's value with something like this:

然后在您的cmbEmployee_SelectionChanged处理程序方法中,您可以使用以下内容更新集合属性的值:

private void cmbEmployee_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    Employees = new ObservableCollection<Employee>();
    string employee = (e.AddedItems[0] as ComboBoxItem).Content as string;
    foreach (DataRow row in newdal2.SelectUser(employee).Tables[0].Rows)
    {
        Employees.Add(new Employee(row.Id, row.Name, row.Whatever));
    }
    Employees = newdal2.SelectUser(employee).Tables[0].DefaultView;
}