How to refresh/reload datagrid in WPF?

后端 未结 3 1582
臣服心动
臣服心动 2021-01-14 23:12

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

But, first code: XAML:

    
    

        
相关标签:
3条回答
  • 2021-01-14 23:24

    dataGrid1.Items.refresh();

    to update the contents of DataGrid

    0 讨论(0)
  • 2021-01-14 23:41

    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.

    or

    • Before assigning new collection try to assign to DataContext null or an empty collection and after actually assign a new one.

    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(..); 
         .
         .
    
    }
    
    • Use UpdateTarget like provided in folowing example: DataBinding Refresh in WPF
    0 讨论(0)
  • 2021-01-14 23:43

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

    dataGridView.ItemsSource = null;
    dataGridView.ItemsSource = ItemsSourceObjects;
    
    0 讨论(0)
提交回复
热议问题