title: c# Update a datagrid view every 5 seconds with a background worker from a MySQL database
body: <p>Here is what I'm trying to achieve:
  I'm building an alert system in a windows form format in c# (visual studio). I have a windows form with a datagrid view that needs to get info from a MySQL database every 5 seconds. I am using a System.Threading.Timer and a background worker. I searched all the threads for this but nothing seems to work. The timer is working because I get a threading eror every 5 seconds so the problem must be in the background worker code. Here is the code:</p>
  
  <pre><code>public partial class Alerts : Form
  {
      private System.Threading.Timer _timerThread;
      private int _period = 5000;
      static BackgroundWorker bgw = new BackgroundWorker();
  
      public Alerts()
      {
          InitializeComponent();
  
          _timerThread = new System.Threading.Timer((o) =&gt;
          {
              // Stop the timer;
              _timerThread.Change(-1, -1);
  
  
              bgw.DoWork += bgw_DoWork;
              bgw.RunWorkerAsync();
  
  
              // start timer again (BeginTime, Interval)
              _timerThread.Change(_period, _period);
          }, null, 0, _period);
  
  
      }
  
      private void bgw_DoWork(object sender, DoWorkEventArgs e)
      {
          string constring = "datasource=localhost;port=3306;username=root;password=*******";
          MySqlConnection conDataBase = new MySqlConnection(constring);
          MySqlCommand cmdDataBase = new MySqlCommand(" select * from shopmanager.alerts ;", conDataBase);
  
          try
          {
              conDataBase.Open();
              MySqlDataAdapter sda = new MySqlDataAdapter();
              sda.SelectCommand = cmdDataBase;
              DataTable dbdataset = new DataTable();
              sda.Fill(dbdataset);
              BindingSource bsource = new BindingSource();
  
              bsource.DataSource = dbdataset;
              dataGridView1.DataSource = bsource;
              sda.Update(dbdataset);
  
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
          conDataBase.Close();
      }
  
      static void bgw_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
      {
          // I think this is where I should be able to update the datagridview but i'm not sure how.
      }
  }
  </code></pre>
  
  <p>I'm completely lost. Please help</p>
  
  <p>I tried this code now but it won't recognize DataGridView1. What am I doing wrong?</p>
  
  <pre><code> public partial class Alerts : Form
  {
      private System.Threading.Timer _timerThread;
      private int _period = 5000;
      static BackgroundWorker bgw = new BackgroundWorker();
      private static object dbdataset;
  
      public Alerts()
      {
          InitializeComponent();
  
          _timerThread = new System.Threading.Timer((o) =&gt;
          {
              // Stop the timer;
              _timerThread.Change(-1, -1);
  
  
              // Calls UpdateAlerts() that updates a datagridview with the mysql data
              bgw.DoWork += bgw_DoWork;
              bgw.RunWorkerAsync();
  
  
              // start timer again (BeginTime, Interval)
              _timerThread.Change(_period, _period);
          }, null, 0, _period);
  
  
      }
  
      private void bgw_DoWork(object sender, DoWorkEventArgs e)
      {
          string constring = "datasource=localhost;port=3306;username=root;password=******";
          MySqlConnection conDataBase = new MySqlConnection(constring);
          MySqlCommand cmdDataBase = new MySqlCommand(" select * from shopmanager.alerts ;", conDataBase);
  
  
          try
          {
              conDataBase.Open();
              MySqlDataAdapter sda = new MySqlDataAdapter();
              sda.SelectCommand = cmdDataBase;
              DataTable dbdataset = new DataTable();
              sda.Fill(dbdataset);
              BindingSource bsource = new BindingSource();
  
              bsource.DataSource = dbdataset;
              sda.Update(dbdataset);
              e.Result = dbdataset;
  
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
          conDataBase.Close();
      }
  
      static void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
      {
          DataGridView1.DataSource = dbdataset;
      }
  }
  </code></pre>