2011-08-24 43 views
0

我希望有人可以帮助我,因为我是一个真正的新手。我有一个绑定到我的webServer上的SQL数据库的WPF应用程序。更新数据库的信息是从自动交易平台完成的。此应用程序旨在实时监控变化。将来自Web服务器的数据绑定到数据网格是没有问题的。然而,我不能为我的生活找出如何实时更新数据网格。我能做的最接近的是添加一个刷新按钮,每次点击它时都会成功更新应用程序。但是,我希望它在数据库发生自动更改时自行更新。有人可以告诉我如何修改我的代码来完成这项工作吗?我已将我的代码放在下面。谢谢!试图通过刷新我的WPF应用程序中的数据集来自动更新我的Datagrid

using System.Windows; 
using Microsoft.Windows.Controls.Primitives; 
using System.Collections.Generic; 
using C1.WPF.DataGrid; 
using StylingWPFGrid.ForexDataSetTableAdapters; 
using System.Windows.Threading; 
using System; 

namespace StylingWPFGrid 
{ 
/// <summary> 
/// Interaction logic for Window1.xaml 
/// </summary> 
public partial class WPFGrid : Window 
{ 

    private ForexDataSet _infoDataSet = null; 
    public ForexDataSet infoDataSet 
    { 
     get 
     { 
      if (_infoDataSet == null) 
      { 
       _infoDataSet = new ForexDataSet(); 
       infoTableAdapter info = new infoTableAdapter(); 
       info.Fill(_infoDataSet.info); 
      } 

      return _infoDataSet; 

     } 
    } 

    private ForexDataSet _tradesDataSet = null; 
    public ForexDataSet tradesDataSet 
    { 
     get 
     { 
      if (_tradesDataSet == null) 
      { 
       _tradesDataSet = new ForexDataSet(); 
       tradesTableAdapter trades = new tradesTableAdapter(); 
       trades.Fill(_tradesDataSet.trades); 
      } 
      return _tradesDataSet; 
     } 
    } 
    public WPFGrid() 
    { 
     InitializeComponent(); this.AccountsDataGrid.ItemsSource = infoDataSet.info; 
     InitializeComponent(); this.TradesDataGrid.ItemsSource = tradesDataSet.trades; 

    } 

    private void QuitBtn_Click(object sender, RoutedEventArgs e) 
    { 
     this.Close(); 
    } 

    private void RefreshBtn_Click(object sender, RoutedEventArgs e) 
    { 
     infoTableAdapter info = new infoTableAdapter(); 
     info.Fill(_infoDataSet.info); 

    } 

} 

}

回答

2

的问题是,你没有什么设置你的属性。所以没有更新。这只是问题的一部分。另一个问题是你需要暗示INotifyPropertyChanged接口。

using System.ComponentModel; 
public partial class WPFGrid : Window, INotifyPropertyChanged 
{ 

    public event PropertyChangedEventHandler PropertyChanged; 

     private void OnPropertyChanged(string propertyName) 
     { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
     } 
} 

然后为你的财产,你应该做的:

public ForexDataSet tradesDataSet 
{ 
    get{//add your code here} 
    set 
    { 
     _tradesDataSet = value; 
     OnPropertyChanged("tradesDataSet"); 
    } 
} 

此外,还要确保您使用的双向绑定,如果你希望你的控制,以更新数据库。

另一个注意事项:你不应该在你的构造函数中要求两次使用InitializeComponent();!它应该是你构造函数中的第一行,另外两个应该在后面。

在计时器上设置它的其他建议也是好主意。我也会尝试这样做,除非你有设置你的数据集的东西。 backgroundworker就是一个很好的例子。

+0

感谢您的反馈,我修复了Initialize Component并为TwoWay设置了绑定。在我的代码中究竟在哪里放置INotifyPropertyChange存在问题,但我会继续保留它。再次感谢。 – Stan

+0

我更新了代码以向您展示应该放置接口的位置。 – Billy

+0

比利,我试图插入一切,因为你已经显示,并只有一个红色的下划线错误,当我这样做,并在该行的最后一个“e”:PropertyChanged(this,new PropertyChangedEventArgs(e));但是,当我运行应用程序时,我没有错误或警告,应用程序出现并且没有刷新。 – Stan

0

数据库中没有信息表示它已更新,我相信,因此您可以使用计时器每秒“单击”按钮。如果数据库无法处理此问题,请尝试编写一个小查询来扫描上次更新,并且只有在数据更改时才抓取数据。

0

您在此应用程序中的数据未绑定到数据库。它绑定到您创建的DataSet。

private ForexDataSet _infoDataSet = null; 
public ForexDataSet infoDataSet 

由于这是一个客户端应用程序,从服务器使用CLR回调函数并不是最好的解决方案。我认为在这种情况下设置“自动刷新”功能就足够了。您可以使用前面建议的计时器。但是,我会在背景线程(Backgroundworker)上执行此操作,因此您在轮询更改时不会影响UI。