Thanks, Stefan! I have tried using the Refresh method, but I can't make it working. As shown in the screenshot, the gridview will not show the latest data in the SQLite database. You can see there are only 9 rows in the gridview which does not match the records number in SQLite database.
My code snippet:
public
partial
class
MainWindow : Window
{
private
const
string
_connStringTemplate =
"metadata=res://*/IomapMonitorModel.csdl|res://*/IomapMonitorModel.ssdl|res://*/IomapMonitorModel.msl;provider=System.Data.SQLite.EF6;provider connection string='data source={0}'"
;
private
const
string
_dbPath =
"t4.iop"
;
private
VirtualQueryableCollectionView _cv;
private
IomapMonitorEntities _context;
private
int
_tick = 0;
public
MainWindow()
{
InitializeComponent();
var _connStr = String.Format(_connStringTemplate, _dbPath);
_context =
new
IomapMonitorEntities(_connStr);
var query = _context.RawRecords.OrderBy(o => o.Id);
_cv =
new
VirtualQueryableCollectionView(query) { LoadSize = 100 };
DataContext = _cv;
StartRefresh();
}
private
void
StartRefresh()
{
var timer =
new
Timer(1000);
timer.Elapsed +=
new
ElapsedEventHandler((obj, e) =>
{
if
(_cv !=
null
)
{
if
(_tick < 20)
{
_context.RawRecords.Add(
new
RawRecord()
{
CardId = 0,
CardType =
"422"
,
ChannelId = 0,
Direction =
"RX"
,
ProtocolType =
"422"
,
Tick = _tick++,
TimeStamp = 11111,
Data =
new
byte
[] { 9, 8, 7 },
Miscellaneous =
null
});
_context.SaveChanges();
}
_cv.Refresh();
}
});
timer.AutoReset =
true
;
timer.Enabled =
true
;
}
}