简体   繁体   English

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

[英]How to refresh/reload datagrid in WPF?

I want to implement a datagrid, which will be filled every 5min.我想实现一个数据网格,每 5 分钟填充一次。

But, first code: XAML:但是,第一个代码: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#: 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;
    }
}

It is very simple.这很简单。 Datagrid use GetEmployees() method to get data. Datagrid 使用GetEmployees()方法来获取数据。

What I try to do, is to run GetEmployess every 5min..我尝试做的是每 5 分钟运行一次 GetEmployess。

PS.附注。 _employeeDataGrid.Items.Refresh(); - doesn't work. - 不起作用。

UPDATE:更新:

Missing classes:缺课:

    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: I need something, that will reload datagrid, so datagrid will have to use GetEmployees method again. UPDATE2:我需要一些东西,它会重新加载数据网格,所以数据网格将不得不再次使用GetEmployees方法。

UPDATE3:更新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);
        }
    }
}

This at start works.这在开始时有效。 But that, I change sth in database, and click the button which do:但是,我更改了数据库中的某个内容,然后单击执行以下操作的按钮:

TaskDataProvider.UpdateMyList();

And on break point, it's gets right data (new data) - but, datagrid is not refreshing:/在断点处,它获取正确的数据(新数据)-但是,数据网格不刷新:/

dataGrid1.Items.refresh(); dataGrid1.Items.refresh();

to update the contents of DataGrid更新 DataGrid 的内容

Or, you could just null ItemsSource out and initialised it again:或者,您可以将ItemsSource并再次初始化它:

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

I gues this cause binding is not aware that you changed collection.我猜这会导致绑定不知道您更改了集合。 I would suggest, or:我会建议,或者:

  • Clear and after populate a collection previously binded to grid with a new data (the same collection, as in GetEmployees() you every time create a new one.清除并使用新数据填充先前绑定到网格的集合(与GetEmployees()相同的集合,您每次都创建一个新数据。

or要么

  • Before assigning new collection try to assign to DataContext null or an empty collection and after actually assign a new one.在分配新集合之前尝试分配给 DataContext null或空集合,然后实际分配一个新集合。

or要么

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM