views:

2146

answers:

5

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.

+1  A: 

Use a timer control and then perform your updates at the specific increments you need.

TheTXI
Note: Timer control's increment can be set in the properties. Then if you double click the control you can access the particular event handler you would need.
TheTXI
+1  A: 

You could create a Timer that fires every 45 seconds, and update your UI from its event handler.

Andy
+2  A: 

Like others have suggested, use a Timer to requery the Database. The only thing I'd like to add is that when you re-query the database, don't just set the DataGridView's datasource to the new table. Rather, Merge it with the existing table. The reason for this is because if the user is in middle of the grid for example looking at a particular row, if you reset the DataSource to a new table, the entire grid will refresh and they will lose their place. Annoying as hell! If you merge it though, it will be seamless to the user.

DataTable.Merge

The one thing to be aware when using the Merge method is that the table needs to have a primary key. Double check to make sure that the DataTable itself has a primary key. Not always does it pull it back from the database. You may need to do something like:

table.PrimaryKey = new DataColumn[] {table.Columns["ID"]};
BFree
@BFree: That is a great tip. Is this something that could be translated to the GridView in ASP.NET as well?
TheTXI
I'm no expert on ASP.NET, but from my limited understanding, don't you have to postback anyway to hit the database again? Wouldn't that cause them to lose their place anyway? Maybe you can somehow cache the currently selected row before postback, and select that row again for the user... Not sure...
BFree
@BFree: you do have to postback, I was just hoping that this was some sort of hidden gem to keep the GridView from rolling back to the top. I knew of MaintainScrollPosition, but it does't work well inside scrollable divs.
TheTXI
+1  A: 
  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.

Daniel LeCheminant
A: 

you mean that i should bind datagridview again in Timer event handler ?

persian Dev