我有一个绑定到BindingListCollectionView(" MyView")的DataGrid,它引用了一个Dataset.table.defaultview。 当我刷新MicrosoftSQL表中的数据时,计时器中的DataTable会更新,但不会更新我的视图中的数据网格。
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 [团队],但这是相同的结果。
我的解决方案有什么问题?为什么我的数据网格没有更新? 合并期间数据集更新(在调试器中看到)。
稍后我想通过使用SqlDependency检测更改来更新数据集。
答案 0 :(得分:0)
如果您调试代码并且
之后数据表中的值正常this.MyView.Refresh();
然后你可以简单地调用
$a=10;
$z=30;
for($prime = $a; $prime<$z; $prime++)
{ if
(Prim($prime) == TRUE)
{ echo $prime."<br/>";}}
我不知道合并是否会引发notifypropertychanged