How to update datagridview in C # every minute

I am currently working on a project in C #, which is pretty simple.

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

When the form loads the data, the GridView fills in correctly.

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

I am looking for suggestions on technology to achieve this. I was looking for clear information, but it seems to be somewhat lacking.

+4
source share
5 answers
  • Add a Timer element to your form. (This is in the component category)
  • Set the Interval property to 45000 (the value represents milliseconds)
  • Either set the Enabled property of the timer to True in the form designer, or somewhere in your code.
  • Add a handler for the Tick timer event (you can get this by double-clicking on the timer)
  • Inside the Tick handler, update your dataGridView

Your handler will look like this:

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

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

+7
source

Like other users, use the Timer to query the database. The only thing I would like to add is that when you re-query the database, do not just set the DataGridView data source to a new table. Rather, merge it with an existing table. The reason for this is that if the user is in the middle of the grid, for example, looking at a certain row, if you reset the DataSource to a new table, the whole grid will be updated, and they will lose their place. Annoying as hell! If you combine it, it will be no problem for the user.

DataTable.Merge

The only thing to know when using the Merge method is that the table must have a primary key. Double check that the DataTable itself has a primary key. It does not always retrieve it from the database. You may need to do something like:

 table.PrimaryKey = new DataColumn[] {table.Columns["ID"]}; 
+5
source

Use timer control, and then perform updates with the desired values.

+1
source

You can create a Timer that starts every 45 seconds and updates your user interface from its event handler.

+1
source

do you mean that I have to bind datagridview again to a Timer event handler?

0
source

All Articles