Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you update a datagridview in C# every minute

I am working on a project in C# at the moment which is quite simple.

I have a status box, two buttons and a dataGridView.

When the Form loads the dataGridView is filled correctly.

What I would like to do is then update that table every 45 seconds to reflect any changes in the database.

I am looking for suggestions on a technique to achieve this. I have been searching for clear information but it seems somewhat lacking.

like image 623
Daniel Mitchell Avatar asked Mar 02 '09 20:03

Daniel Mitchell


1 Answers

  1. Add a Timer control to your form. (It's in the components category)
  2. Set its Interval property to 45000 (the value represents milliseconds)
  3. Either set the Enabled property of the timer to True in the form designer, or somewhere in your code.
  4. Add a handler for the timer's Tick event (you can get this by double-clicking the timer)
  5. Inside the Tick handler, update your dataGridView

Your handler will look like this:

private void timer1_Tick(object sender, EventArgs e)
{
    // Update DataGridView
}

If you need to suspend updates for some reason, you can call timer1.Stop() to stop the timer from running, and use timer1.Start() to start it up again.

like image 53
Daniel LeCheminant Avatar answered Oct 10 '22 02:10

Daniel LeCheminant