本文介绍了如何刷新在WPF /重装数据网格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个数据网格,这将填补每5分钟。

I want to implement a datagrid, which will be filled every 5min.

不过,第一个code:XAML:

But, first code: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每5分钟。

What I try to do, is to run GetEmployess every 5min..

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

PS. _employeeDataGrid.Items.Refresh(); - doesn't work.

更新:

缺少类:

    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:我需要的东西,这将重新加载数据网格,使数据网格将不得不再次使用装getEmployees 方法。

UPDATE2:I need something, that will reload datagrid, so datagrid will have to use GetEmployees method again.

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

这在启动工作。但是,我改变......在数据库中,并点击里面做的按钮:

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:/

推荐答案

或者,你可以只空的ItemsSource 出来,再初始化它:

Or, you could just null ItemsSource out and initialised it again:

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

这篇关于如何刷新在WPF /重装数据网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 16:16