【发布时间】:2016-06-10 06:42:19
【问题描述】:
我有一个绑定到引用 Dataset.table.defaultview 的 BindingListCollectionView(“MyView”)的 DataGrid。 当我刷新 MicrosoftSQL 表中的数据时,计时器中的数据表会更新,但我视图中的数据网格不会更新。
XAML:
<DataGrid Name="dataGrid1" IsReadOnly="True" ItemsSource="{Binding Path=MyView, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" KeyDown="dataGrid1_KeyDown" />
后面的代码:
public partial class MainWindow : Window, INotifyPropertyChanged
{
private DataSet _exhibitionTeam;
private static string CONN_STRING = @"Server=testServer;Database=TestDB;Uid=TestUser;Pwd=TestPwd;";
string SELECT_SQL = "SELECT kurzzeichen,name,mail,telefon from pers_didacta";
BackgroundWorker worker;
private BindingListCollectionView _view;
public BindingListCollectionView MyView
{
get { return this._view; }
protected set
{
this._view = value;
//this.NotifyPropertyChanged(() => this.MyView);
RaisePropertyChanged("MyView");
}
}
public MainWindow()
{
InitializeComponent();
DataContext = this;
_exhibitionTeam = new DataSet("dsExhibitionTeam");
ReadDataIntoDataSet();
this.MyView = (BindingListCollectionView)CollectionViewSource.GetDefaultView(_exhibitionTeam.Tables["Team"].DefaultView);
worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.RunWorkerAsync();
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
while (true)
{
RefreshDataIntoDataSet();
this.MyView.Refresh();
Thread.Sleep(2000);
}
}
public DataSet ExhibitionTeam
{
get
{
return _exhibitionTeam;
}
set
{
_exhibitionTeam = value;
RaisePropertyChanged("ExhibitionTeam");
}
}
private void ReadDataIntoDataSet()
{
using (SqlConnection con = new SqlConnection(CONN_STRING))
{
SqlCommand cmd = new SqlCommand(SELECT_SQL, con);
SqlDataAdapter myMySqlDataAdapter = new SqlDataAdapter(cmd);
myMySqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
myMySqlDataAdapter.Fill(_exhibitionTeam, "Team");
myMySqlDataAdapter.Dispose(); //not needed any longer!
}
}
private void RefreshDataIntoDataSet()
{
using (SqlConnection con = new SqlConnection(CONN_STRING))
{
con.Open();
DataSet myDataSet;
SqlCommand cmd = new SqlCommand(SELECT_SQL, con);
SqlDataAdapter myMySqlDataAdapter = new SqlDataAdapter(cmd);
myMySqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
myDataSet = new DataSet("dsExhibitions");
myMySqlDataAdapter.Fill(myDataSet, "Team");
ExhibitionTeam.Merge(myDataSet, false);
myMySqlDataAdapter.Dispose(); //not needed any longer!
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
private void dataGrid1_KeyDown(object sender, KeyEventArgs e)
{
RefreshDataIntoDataSet();
}
}
当我按下数据网格上的任意键时,数据会更新。但是我的后台工作人员的相同方法将不起作用。
我也尝试绑定 ExhibitionTeam.Tables[Team] 但结果相同。
我的解决方案有什么问题?为什么我的数据网格没有更新? 合并期间数据集更新(在调试器中可见)。
稍后我想通过使用 SqlDependency 检测更改来更新数据集。
【问题讨论】:
标签: wpf datagrid notifydatasetchanged