Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7033985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:07:51+00:00 2026-05-28T01:07:51+00:00

I modified the code, but I m still in trouble, all it’s fine. Except

  • 0

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");
        }


    }
}  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:07:52+00:00Added an answer on May 28, 2026 at 1:07 am

    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:

    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 <= SystemTime.TimeOfDay.Ticks && SystemTime.TimeOfDay.Ticks < 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"); 
            } 
    
    
        } 
      } 
    

    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 using statement around your filestream code than what you have done)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran across this chunk of code (modified) in our application, and am confused
I want to rewrite this code without so many else's, but still keep it
Yesterday I submitted this problem, got terrific responses but my code still didn't work.
I have the MySQL Connector/NET installed on my PC. I modified the source code
I've taken code from the MSDN page on Matrix Filters and modified it slightly.
I modified an existing form and saved it on my desktop as .oft file.
Has anyone ever modified the post-new.php file in their WordPress installation? I want to
I have an app modified to take into account the UAC in VISTA. So,
i've been modified the code like below <script type=text/javascript> function addtext() { var barCode
This question is kind of anecdotical but still interesting to me; I was wondering

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.