如何刷新/重新加载WPF中的数据网格?

问题描述:

我想实现一个数据网格,它将每5分钟填充一次。如何刷新/重新加载WPF中的数据网格?

但是,第一码: 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()方法获取数据。

我尝试做的,就是运行GetEmployess每隔5min ..

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

UPDATE:

缺少类:

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

} 

UPDATE2: 我需要的东西,这将重新加载数据网格,以使DataGrid将不得不再次使用GetEmployees方法。

UPDATE3:

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

而且在破发点,这是得到正确的数据(新数据) - 但是,数据网格是不爽快:/

+0

你能请,张贴代码,您实际上分配到的DataContext收集了来自GetEmployees()? – Tigran

+0

我做到了。就这样。只有缺失的代码有两种方法: UIEmployee和ObservableEmployee。 Datagrid使用父Grid的DataContext,所以“_employeeFactory”和这个类使用方法“GetEmployees”,它返回ObservableEmployee。没有更多的代码。 – Marshall

或者,你可以只空ItemsSource并再次初始化它:

dataGridView.ItemsSource = null; 
dataGridView.ItemsSource = ItemsSourceObjects; 

我觉得这个原因绑定不知道你改变了收藏。 我建议,或:

  • 清除和填充后的集合先前绑定到一个新的数据网格(同一个集合,如GetEmployees()你每次创建一个新的

  • 之前分配新的集合尝试分配到的DataContext null或空集,实际上后分配一个新的。

EDIT

ObservableEmployee <UIEmployee> list = new ObservableEmployee <UIEmployee>(); //move list to global variables and declare it as ObservableCollection 
public ObservableEmployee GetEmployees() 
    { 

     //here only return an instance of list 
     return list; 

    } 

//this method call when you want to update data on gri 
public void UpdateMyList() { 

    //clear list 
    list.Clear(); 

    //after add all data 
    list.Ad(..); 
    list(..); 
    . 
    . 

} 
+0

对不起,但我不知道如何,如果前两个建议。 第三,我读了整个话题,但仍然无法解决这个问题。我几天前才开始使用WPF .. 你能帮我,并在我的代码中显示这个吗? – Marshall

+0

如果每5秒钟后需要更新DataGrid的内容,则不要每次都在GetEmployees()方法中创建新列表。但是实例一是全局的,并且在该方法内每5秒刷新一次新数据。 – Tigran

+0

嗨,thx重播。 但我的问题是,我不知道如何刷新列表:/ 我有“GetEmployees”方法内的断点,并且在调试期间它只停留一次(在开始处)。 比我称之为“_employeeDataGrid.Items.Refresh();”和“GetEmployees”不被调用。 – Marshall

dataGrid1.Items。刷新();

更新DataGrid的内容