I modified the code, but I m still in trouble, all it’s fine. Except when I modify the data into the XML file, the application crash. it Should be refresh the datagridview when I modify the data into the xml file.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.IO;
using System.Threading;
using System.Reflection;
namespace XML
{
public partial class Form1 : Form
{
DataSet formBindingSource = null;
public Form1()
{
InitializeComponent();
//
formBindingSource = new DataSet();
using (FileStream stream1 = new FileStream("c:\\sites.xml", FileMode.Open))
{
formBindingSource.ReadXml(stream1);
}
this.UpdateDataGrid();
dataGridView1.DataSource = formBindingSource.Tables[0];
//
this.timer1.Enabled = true;
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
FileSystemWatcher incoming = new FileSystemWatcher();
incoming.Path = @"c:\";
incoming.NotifyFilter = NotifyFilters.LastAccess |
NotifyFilters.LastWrite |
NotifyFilters.FileName;
incoming.Filter = "sites.xml";
incoming.Changed += new FileSystemEventHandler(OnChanged);
incoming.EnableRaisingEvents = true;
//
//
}
public void OnChanged(object source, FileSystemEventArgs e)
{
using (FileStream stream1 = new FileStream("c:\\sites.xml", FileMode.Open))
{
formBindingSource.ReadXml(stream1);
}
this.UpdateDataGrid();
dataGridView1.DataSource = formBindingSource.Tables[0];
}
public void UpdateDataGrid()
{
if (this.InvokeRequired)
{
this.Invoke((MethodInvoker)delegate { UpdateDataGrid(); });
}
else
{
//refresh column status evry second
int count = 0;
foreach (DataRow dr in formBindingSource.Tables[0].Rows)
{
DateTime SystemTime = Convert.ToDateTime(DateTime.Now);
DateTime StartTime = Convert.ToDateTime(dr[0]);
DateTime EndTime = Convert.ToDateTime(dr[1]);
if (StartTime.TimeOfDay.Ticks <= SystemTime.TimeOfDay.Ticks && SystemTime.TimeOfDay.Ticks < EndTime.TimeOfDay.Ticks)
{
formBindingSource.Tables[0].Rows[count][5] = "ok";
}
else
{
formBindingSource.Tables[0].Rows[count][5] = "nok";
}
count++;
}
formBindingSource.Tables[0].DefaultView.RowFilter = "date = #" + DateTime.Today + "#";
}
}
private void timer1_Tick(object sender, EventArgs e)
{
this.UpdateDataGrid();
this.label1.Text = DateTime.Now.ToString("dddd, MMMM dd, yyyy hh:mm:ss tt");
}
}
}
It looks like you are just updating a column every second. It might be more efficient to make the datatable a property of the form and update that every second (i.e. there should be no need to reset the data source on the grid…this might be causing you a problem as it fires a lot of events when you do that).
As per my comment, it looks like you only need to reload the datatable when the file system watcher event fires, and that should be the only time you rebind to your grid.
In response to your comment your code should look something like below:
Notice how there is a form level dataset called formBindingSource. When you update that your gird should update automatically without you having to reset the datasource for the grid. You only need to rebind when the file changes and you load a new dataset.
(Also, its easier to use a
usingstatement around your filestream code than what you have done)