【问题标题】:How to refresh/reload datagrid in WPF?如何在 WPF 中刷新/重新加载数据网格?
【发布时间】:2011-08-27 15:13:49
【问题描述】:

我想实现一个数据网格,它将每 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。

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

更新:

缺课:

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

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

【问题讨论】:

  • 能否请您发布一个代码,在其中实际分配从 GetEmployees() 获得的集合到 DataContext ?
  • 我做到了。就这样。唯一缺少的代码是两个方法:UIEmployee 和 ObservableEmployee。 Datagrid 使用父 Grid 的 DataContext,因此“_employeeFactory”和此类使用返回 ObservableEmployee 的方法“GetEmployees”。没有更多代码了。

标签: c# .net wpf xaml


【解决方案1】:

dataGrid1.Items.refresh();

更新 DataGrid 的内容

【讨论】:

    【解决方案2】:

    或者,您可以将 ItemsSource 设为 null 并再次对其进行初始化:

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

    【讨论】:

      【解决方案3】:

      我猜这是因为绑定不知道您更改了集合。 我会建议,或者:

      • 清除并在使用新数据填充先前绑定到网格的集合之后(与GetEmployees() 相同的集合,您每次都创建一个新数据。

      • 在分配新集合之前尝试分配给 DataContext null 或空集合,然后实际分配一个新集合。

      编辑

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

      【讨论】:

      • 我很抱歉,但我不知道如何,以防前两个建议。第三,我阅读了整个主题,但仍然无法解决这个问题。几天前我才开始使用 WPF。你能帮我,并在我的代码上显示这个吗?
      • 如果每 5 秒后您需要更新 DataGrid 的内容,请尽量不要每次都在 GetEmployees() 方法中创建新列表。但是实例一个全局,并且在该方法中每 5 秒后用新数据刷新列表。
      • 嗨,谢谢重播。但我的问题是我不知道如何刷新列表:/我在“GetEmployees”方法中有断点,并且在调试期间它只停在那里一次(在开始时)。比我称之为“_employeeDataGrid.Items.Refresh();”并且没有调用“GetEmployees”。
      • @Marshall:查看我编辑的帖子。 List 被声明为 ObservableCollection,在 GetEmployess 中它只返回相同的列表,并且不会每次都创建一个新列表,并且有一种更新列表内容的方法。所以考虑到 list 是 ObservableCollection 并且它绑定到网格的事实,在 irdr 中更新 grdi 内容,你只需要更改列表数据,其他一切都将通过 WPF 绑定机制完成。
      • @PintuPaul:尝试重置数据绑定。数据绑定在内部保存了一个指向您首先分配的数据结构的指针,方法是设置为 nul 您可以休息它,以便新的分配可能会生效。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-04
      • 2019-02-09
      • 1970-01-01
      • 2013-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多