I've applied the MVVM pattern in c# using Windows Forms. In the View (MainForm) I bind a DataGridView "InstalledChartsView" to a DataTable "ChartTable".
BindingSource binding = new BindingSource
{
DataSource = m_ViewModel.ChartTable
};
InstalledChartsView.DataSource = binding;
This ChartTable is part of the "Charts" class created in the ViewModel:
private Charts m_Charts;
public DataTable ChartTable
{
get { return m_Charts.ChartTable; }
set
{
m_Charts.ChartTable = value;
NotifyPropertyChanged("ChartTable");
}
}
Now what really should be happening is that m_Charts (the model), a thread is running that is continuously updating this ChartTable from an online database. The DataTable gets updated correctly, but the gridview does not.
However, for testing, I'm not even touching the model, but doing some stuff from the view (MainForm). I'm continuously running the following thread and weird stuff happens there.
new Thread( () =>
{
for ( ; ; )
{
m_ViewModel.AddRow("b", "b", "b", "b");
//Some unrelated stuff happens here.
try
{
BeginInvoke( (Action) ( () =>
{
m_ViewModel.AddRow("a","a","a","a");
}
}
catch{};
Thread.sleep();
}
}
Now both "AddRow" comments get executed more or less correctly: a row is added to the table in the model. However, the view ONLY gets updated at the aaaa statement! At that time BOTH rows get shown correctly.
This is my addRow method:
public void AddRow(string a, string b, string c, string d)
{
m_Charts.ChartTable.Rows.Add(a, b, c, d);
NotifyPropertyChanged("ChartTable");
}
Can anyone shed some light on why NotifyPropertyChanged works correctly in one case, but not in the other?