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 8404433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:35:58+00:00 2026-06-09T22:35:58+00:00

I want to refresh my datagridview if there are changes in a particular xml

  • 0

enter image description here
I want to refresh my datagridview if there are changes in a particular xml file. I got a FileSystemWatcher to look for any changes in the file and call the datagirdview function to reload the xml data.

When i tried, i’m getting Invalid data Exception error Somebody please tell what is the mistake am i doing here??

  public Form1()
            {
                InitializeComponent();
                FileSystemWatcher watcher = new FileSystemWatcher();

                watcher.Path = @"C:\test";
                watcher.Changed +=  fileSystemWatcher1_Changed;
                watcher.EnableRaisingEvents = true;
                //watches only Person.xml
                watcher.Filter = "Person.xml";

                //watches all files with a .xml extension
                watcher.Filter = "*.xml";

            }

            private const string filePath = @"C:\test\Person.xml";
            private void LoadDatagrid()
            {
                try
                {
                    using (XmlReader xmlFile = XmlReader.Create(filePath, new XmlReaderSettings()))
                    {
                        DataSet ds = new DataSet();
                        ds.ReadXml(xmlFile);
                        dataGridView1.DataSource = ds.Tables[0]; //Here is the problem
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                } 
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                LoadDatagrid();
            }

private void fileSystemWatcher1_Changed(object sender, FileSystemEventArgs e)
            {
                LoadDatagrid();
            }
  • 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-06-09T22:35:59+00:00Added an answer on June 9, 2026 at 10:35 pm

    This is because the FileSystemWatcher runs on a distinct thread, not the UI thread. On winforms apps only the UI thread – the main thread of the program – can interact with visual constrols. If you need to interact with visual controls from another thread – like this case – you must call Invoke on the target control.

     // this event will be fired from the thread where FileSystemWatcher is running.
     private void fileSystemWatcher1_Changed(object sender, FileSystemEventArgs e)
     {
          // Call Invoke on the current form, so the LoadDataGrid method
          // will be executed on the main UI thread.
          this.Invoke(new Action(()=> LoadDatagrid()));
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to refresh icon for particular file/folder in Finder application. FNNotifyByPath( (const UInt8
I want to auto refresh result div with ajax & setInterval function. But there
I just want to know this code of writing to refresh a datagridview when
I want to refresh with new hash but it seems chrome just changes the
I have a dynamic listview and I want to refresh the list when any
I want to refresh the activity as i want thatwithout firing any event some
hello every one i want to refresh my listview after adding or removing any
Is there something like UpdatePanel (in ASPX) for Razor? I want to refresh data
I want to sort my DataGridView. I tried this: Grid.DataSource = PlayerList; Grid.Refresh(); and
I've got a java.util.List bind to a JTable, if I want to refresh the

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.