dynamically updating a datagrid from a XML file c# 3.5
I have a datagrid that is populated via XML file when the form loads. Everything is working great but I would like the datagrid to update dynamically when a new order is received (Separate class receives a data stream and updates the file).
Im looking for suggestions on how this should be done. (ie using a timer update every second, or monitor the file using FileSystemWatcher.. etc)
Since where here, I might as well mention that in order to update the datagrid Im clearing the whole data set and Re-reading the file using:
DataSet.Clear()开发者_StackOverflow社区;
DataSet.ReadXml("file.xml");
dataGridView1.DataSource = DataSet;
if this is not the proper approach, please offer any alternate suggestions.
Seems like a file watch on your file.xml would do the trick here. I would try something along these lines:
FileSystemWatcher incoming = new FileSystemWatcher();
incoming.Path = @"c:\locationDirectory\";
incoming.NotifyFilter = NotifyFilters.LastAccess |
NotifyFilters.LastWrite |
NotifyFilters.FileName;
incoming.Filter = "file.xml";
incoming.Changed += new FileSystemEventHandler(OnChanged);
incoming.EnableRaisingEvents = true;
In your OnChanged event you can setup the re-bind of your dataGrid. That seems like it would be the simplest thing that could work.
精彩评论