Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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). </p> <p>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.</p> <p>In response to your comment your code should look something like below:</p> <pre><code>namespace XML { public partial class Form1 : Form { DataSet formBindingSource = null; public Form1() { InitializeComponent(); 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) { formBindingSource = new DataSet(); using(FileStream stream1 = new FileStream("c:\\sites.xml", FileMode.Open)) { ds.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 &lt;= SystemTime.TimeOfDay.Ticks &amp;&amp; SystemTime.TimeOfDay.Ticks &lt; EndTime.TimeOfDay.Ticks) { ds.Tables[0].Rows[count][5] = "ok"; } else { ds.Tables[0].Rows[count][5] = "nok"; } count++; } formBindingSource.Tables[0].DefaultView.RowFilter = "date = #" + DateTime.Today + "#"; } } private void Form1_Load(object sender, EventArgs e) { //Load and bind file OnChanged(null,null) } private void timer1_Tick(object sender, EventArgs e) { this.UpdateDataGrid(); this.label1.Text = DateTime.Now.ToString("dddd, MMMM dd, yyyy hh:mm:ss tt"); } } } </code></pre> <p>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.</p> <p>(Also, its easier to use a <code>using</code> statement around your filestream code than what you have done)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload