我有一個DataGrid
在C#WPF
填充了Employees
。基本上我需要定期讀取Database
,因爲當Employees
進入建築物時,字段被修改。處理這種情況的最佳方法是處理幾個問題。首先,這是我如何定義我的DataGrid
;定期刷新DataGrid C#WPF
public ICollectionView FilteredView { get; set; }
public ObservableCollection<EmployeeModel> Employees {get; set; }
private void OnPageLoad(object sender, RoutedEventArgs e)
{
var _employeeDataService = new EmployeeDataService();
Employees = _employeeDataService.HandleEmployeeSelect();
FilteredView = CollectionViewSource.GetDefaultView(Employees);
dataGrid.ItemsSource = FilteredView;
DataContext = this;
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(TimerTick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
dispatcherTimer.Start();
}
本質當前觸發間隔爲第二隻需再次調用該方法,使得整個表從數據庫和相應的字段中檢索到的TimerTick
方法(用戶是否是在建築物或不被更新)。
正如我所說的這種方法有很多問題。首先,它看起來不太好。由於DataGrid
不斷刷新,用戶在網格上執行的任何操作都很慢,並且每行的高亮顯示都會失真。
其次,我使用方法來篩選DataGrid
。用戶能夠鍵入Employee
的名稱,並且相應地過濾DataGrid
。再次因爲DataGrid
每秒刷新一次,因此幾乎不可能對DataGrid
進行過濾,因爲用戶輸入的內容每秒都會被撤消。
據我所知,我可能會刷新DataGrid
說每分鐘會有很大改善,但它仍然沒有接近理想的地方,我相信有更好的解決方案。如何改進此解決方案,以便更新DataGrid
對用戶來說更加微妙和不爲人知?
問題:爲什麼你需要經常刷新DataGrid? 你只是更新屬性或每秒重新設置視圖的datacontext? –
我的建議是,你不能從數據庫中獲取所有數據。首先檢查數據庫中是否發生了變化。如果該函數比僅從數據庫獲取數據成真。 (使用線程獲取數據) –
@VivekSaurav它基本上是員工進入/離開建築物的監控系統。目前我正在重新設置datacontext,但我想這是爲了避免這種情況。 – CBreeze