c# - 如何在 WPF 中刷新/重新加载数据网格?

标签 c# .net wpf xaml

我想实现一个数据网格,每 5 分钟填充一次。

但是,第一个代码: XAML:

    <Window.Resources>
    <ObjectDataProvider x:Key="_employeeProvider" ObjectType="{x:Type Brw1:EmployeeDataProvider}" />
    <ObjectDataProvider x:Key="_employeeFactory" ObjectInstance="{StaticResource _employeeProvider}" MethodName="GetEmployees" />
</Window.Resources>

<Grid DataContext="{Binding Source={StaticResource _employeeFactory}}">
    <DataGrid Name="_employeeDataGrid" DockPanel.Dock="Top" Margin="12,57,12,0" VerticalAlignment="Top" ItemsSource="{Binding}" AutoGenerateColumns="True">
    </DataGrid>
</Grid>

C#:

    public class EmployeeDataProvider
{
    public ObservableEmployee GetEmployees()
    {
        // This is only example
        // In my project right here I will get data from database, put into the list, and fill datagrid with it. 
        List<UIEmployee> list = new List<UIEmployee>
                {
                    new UIEmployee { Id = 1, FirstName = "Prajeesh2", LastName = "Prathap", Occupation = "Engineer", DateOfJoining=new DateTime(2005, 8, 1), IsContracter = false, AddressInfo = null },
                    new UIEmployee { Id = 2, FirstName = "Rahul2", LastName = "Bose", Occupation = "Student", DateOfJoining=new DateTime(2009, 5, 4), IsContracter = true, AddressInfo = null },
                    new UIEmployee { Id = 3, FirstName = "Steve2", LastName = "Roberts", Occupation = "Manager", DateOfJoining=new DateTime(1994, 8, 23), IsContracter = false, AddressInfo = null },
                    new UIEmployee { Id = 4, FirstName = "Micheal2", LastName = "Clarke", Occupation = "Engineer", DateOfJoining=new DateTime(2003, 1, 14), IsContracter = true, AddressInfo = null },
                    new UIEmployee { Id = 5, FirstName = "Rachel2", LastName = "Green", Occupation = "Professional", DateOfJoining=new DateTime(2006, 3, 8), IsContracter = true, AddressInfo = null }
                };

        ObservableEmployee employeeCollection = new ObservableEmployee(list);
        return employeeCollection;
    }
}

非常简单。 Datagrid使用GetEmployees()方法获取数据。

我尝试做的是每 5 分钟运行一次 GetEmployess..

附言。 _employeeDataGrid.Items.Refresh(); - 不起作用。

更新:

缺少类(class):

    public class ObservableEmployee : ObservableCollection<UIEmployee>
{
    public ObservableEmployee(IEnumerable<UIEmployee> employees) : base(employees) { }
}

public class UIEmployee// : INotifyPropertyChanged
{
    private int m_Id;
    public int Id
    {
        get { return m_Id; }
        set
        {
            m_Id = value;
        }
    }

    private string m_FirstName;
    public string FirstName
    {
        get { return m_FirstName; }
        set
        {
            m_FirstName = value;
        }
    }

    private string m_LastName;
    public string LastName
    {
        get { return m_LastName; }
        set
        {
            m_LastName = value;
        }
    }

    private string m_Occupation;
    public string Occupation
    {
        get { return m_Occupation; }
        set
        {
            m_Occupation = value;
        }
    }

    private DateTime m_DateOfJoining;
    public DateTime DateOfJoining
    {
        get { return m_DateOfJoining; }
        set
        {
            m_DateOfJoining = value;
        }
    }

    private bool m_IsContracter;
    public bool IsContracter
    {
        get { return m_IsContracter; }
        set
        {
            m_IsContracter = value;
        }
    }

}

更新 2: 我需要一些东西,它将重新加载数据网格,因此数据网格将不得不再次使用 GetEmployees 方法。

更新 3:

public class TaskDataProvider
{
    static ObservableEmployee list = new ObservableEmployee();
    static DataRepository dr = new DataRepository();

    public static ObservableEmployee GetEmployees()
    {
        UpdateMyList();
        return list;
    }

    public static void UpdateMyList() 
    {
        ObservableTask newList = new ObservableTask(dr.GetTasks());

        list.Clear();
        foreach (Task t in newList)
        {
            list.Add(t);
        }
    }
}

这在开始时有效。 但是,我在数据库中更改了某事,然后单击执行以下操作的按钮:

TaskDataProvider.UpdateMyList();

在断点处,它获取了正确的数据(新数据)——但是,数据网格没有刷新:/

最佳答案

dataGrid1.Items.refresh();

更新DataGrid的内容

关于c# - 如何在 WPF 中刷新/重新加载数据网格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7215409/

相关文章:

wpf - 禁用对 WPF MVVM 中 DataGrid 中自动生成的列进行排序

c# - WPF 应用程序,NotifyIcon(窗体)问题

c# - 在 MSBuild 的解决方案中循环项目

c# - WPF MVVM 模型中的按钮命令

C# - 大型集合存储

c# - ParameterInfo.ParameterType 是 "SqlBytes&",如何与 typeof() 比较

c# - 正确设置窗口图标并发生 XamlParseException

c# - NewGuid 与 System.Guid.NewGuid().ToString ("D");

c# - 将窗体控件绑定(bind)到对象的属性

c# - 高可用性