How to automatically refresh multiple tabs on one datagrid
So I have a an app that has multiple tabs that share one datagrid. I am trying to figure out how to get datagrid to update automatically every hour. Each tab is displaying results of a query from sql server. All the tabs will need to update each hour, as well 开发者_运维知识库as, after a new item is added to the database. Let me know if you need more info.
If you are using WPF, just bind the DataGrid to a shared DataSource (ObservableCollection). Update your collection every so often and the DataGrid will automatically update.
XAML:
<DataGrid x:Name="DataGrid1" ItemsSource={Binding Path=SharedCollection, Mode=OneWay} .../>
<DataGrid x:Name="DataGrid2" ItemsSource={Binding Path=SharedCollection, Mode=OneWay} .../>
<DataGrid x:Name="DataGrid3" ItemsSource={Binding Path=SharedCollection, Mode=OneWay} .../>
Code Behind:
public class View : Window
{
public View()
{
this.DataContext = new ViewModel();
}
}
View Model:
public class ViewModel : INotifyPropertyChanged
{
public ObservableCollection<MyType> SharedCollection = new ObservableCollection<MyType>();
...
...
public void UpdateData()
{
SharedCollection.Clear();
var data = GetMyDataFromSQLQuery();
foreach( var item in data )
{
SharedCollection.Add( item );
}
}
}
精彩评论