Reputation: 461
I have gone through many questions of same type, but couldn't figure out the right solution. I have implemented the INotifyPropertyChanged
for my MainViewModel.cs
class to see whether my UI is updated when my source changes,but there is no effect when I run my application.
This is my Xaml code :
<Window.DataContext>
<ViewModel:MainViewModel/>
</Window.DataContext>
<Grid>
<DataGrid ItemsSource="{Binding Path=SystemStatusData,Mode=OneWay,UpdateSourceTrigger=PropertyChanged,NotifyOnTargetUpdated=True}"
AutoGenerateColumns="False" Height="287" HorizontalAlignment="Left" Margin="12,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="479" >
my MainViewModel.cs class :
namespace MVVM_DemoAppl.ViewModels
{
public class MainViewModel : INotifyPropertyChanged
{
Model _myModel = new Model();
private ObservableCollection<SystemStatus> _systemStatusData= new ObservableCollection<SystemStatus>();
public ObservableCollection<SystemStatus> SystemStatusData
{
get { return _systemStatusData; }
set
{
_systemStatusData= value;
OnPropertyChanged("SystemStatusData");
}
}
public MainViewModel()
{
initializeload();
}
private void initializeload()
{
DataTable table = _myModel.getData();
for (int i = 0; i < table.Rows.Count; ++i)
SystemStatusData.Add(new SystemStatus
{
Systems= table.Rows[i][0].ToString(),
Date =Convert.ToDateTime(table.Rows[i][1]),
Types = table.Rows[i][2].ToString(),
Messages = table.Rows[i][3].ToString()
Critical = Convert.ToBoolean(table.Rows[i][1]),
});
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyname)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyname));
}
}
public class Model
{
string con = ConfigurationManager.AppSettings["ConnectionStrings"];
public DataTable getData()
{
DataTable ndt = new DataTable();
SqlConnection sqlcon = new SqlConnection(con);
sqlcon.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from test_DB.dbo.SystemStatus",con);
da.Fill(ndt);
return ndt;
}
}
}
Upvotes: 3
Views: 1914
Reputation: 8147
SQL Server does not automatically update your resultset after the query has first run. The most basic approach to achieve what you are after would be to poll the server to check for changes at set intervals and update the screen from there.
You could look into CDC which would help identify what has changed if anything. None of this is automatic though and you will need to re-run your query each time.
An example of how a timer could work:
// Background timer used to refresh...
private DispatcherTimer _timer = null;
public MainViewModel()
{
// Do initial load
initializeload();
// Start the timer to refresh every 100ms thereafter (change as required)
_timer = new DispatcherTimer();
_timer.Tick += Each_Tick;
_timer.Interval = new TimeSpan(0, 0, 0, 0, 100);
_timer.Start();
}
// Raised every time the timer goes off
private void Each_Tick(object o, EventArgs sender)
{
// Refresh from database etc ...
initializeload();
// Anything else you need ...
}
private void initializeload()
{
// Your existing code here ...
}
Upvotes: 2