【问题标题】:How to automatically refresh multiple tabs on one datagrid如何自动刷新一个数据网格上的多个选项卡
【发布时间】:2011-06-10 13:27:40
【问题描述】:

所以我有一个应用程序,它有多个共享一个数据网格的选项卡。我试图弄清楚如何让数据网格每小时自动更新。每个选项卡都显示来自 sql server 的查询结果。所有选项卡都需要每小时更新一次,以及在将新项目添加到数据库之后。如果您需要更多信息,请告诉我。

【问题讨论】:

    标签: c# sql wpf datagrid tabs


    【解决方案1】:

    如果您使用 WPF,只需将 DataGrid 绑定到共享数据源 (ObservableCollection)。每隔一段时间更新您的集合,DataGrid 会自动更新。

    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} .../>
    

    代码背后:

    public class View : Window
    {
        public View()
        {
            this.DataContext = new ViewModel();
        }
    }
    

    查看模型:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-15
      • 1970-01-01
      • 2021-08-28
      • 2012-05-26
      • 2017-02-02
      • 1970-01-01
      相关资源
      最近更新 更多