Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
First I am new to building windows forms and a novice script writer (youtube taught) so please bear with me. I have created a windows form in Visual Studio that allows the user to add, modify and delete data in a sql table and it displays these changes in a gridview that is attached to the windows form. I am able to display the changes to the sql table instantly from the form I made the changes to, the problem is if multiple users have the form open they do not see the changes until they either make a change themselves or close and reopen the form. I am looking for a way so if one person makes a change, all users that have the form open will also see the change automatically without having to manual refresh the screen. I found a tool in Visual Studio for timer that appears to work by refreshing the gridview x amount of seconds. I tried to attach this to my gridview but get an error "No overload for 'timer_Tick' matches delegate 'EventHandler'". First is this the way to go or should I use something else to auto refresh the gridview? If it is what am I doing wrong.

What I have tried:

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
       {
           Timer timer = new Timer();
           timer.Interval = (10 * 1000); // 10 secs
           timer.Tick += new EventHandler(timer_Tick);
           timer.Start();
       }

       private void timer_Tick(object sender, DataGridViewCellEventArgs e)
       {
           //refresh here...
       }
Posted
Updated 19-Dec-17 4:25am
v2

1 solution

Wow. Where to begin...

First, you're creating a new Timer on every cell click. THAT'S REALLY BAD! Timers are a finite resource in Windows and you're not destroying the ones you create, so you're eventually going to have dozens of Timers running, all doing the same thing!

Using a Timer is still possible, IF AND ONLY IF the user doesn't get the chance to do any editing in the grid you're trying to refresh. Refreshing it while editing will cancel the users editing, right in them middle of them doing it.

But, using a Timer for refreshing something from the database has a downside, scalability. The more clients you have refreshing on a Timer the more queries you're going to have hitting the database. It'll get to the point where the clients are just flooding the database with requests for the same data.

Also, if you're retrieving a large number of records to shove in the DataGridView, you're putting a larger load on the database and on your clients. A data bind is not a lightweight, quick operation.

To solve that problem, you'd have to use paging or virtual mode in the grid. That complicates things.

Second, look at the signature of the timer_Tick event handler you're trying to wire up:
private void timer_Tick(object sender, DataGridViewCellEventArgs e)

The event arguments are for a DataGridView event, not for the Timer Tick event. That's why you're getting the exception you are. The Timer Tick event just uses the generic EventArgs argument:
private void timer_Tick(object sender, EventArgs e)


To use a Timer, you'd put the timer on the form and double click it to get the event handler started for you. Do your database data retrieval and grid refresh from in there. Start the Timer when your form is shown and you're done. There is no need to create a new timer on every cell click.

There are other options to using a Timer but the code becomes more and more complicated, such as using SQL Server Notifications (old), server-side components, caching and subscriptions, services, blah, blah, blah, ... nothing something for the "new to this" crowd.
 
Share this answer
 
Comments
Member 13581008 19-Dec-17 13:16pm    
Thanks for info. I am not attaching a timer to every click. I just need the gridview to refresh. So if you don't recommend using a timer can you point me to what I should be using?

Also, I tried your recommendation on the timer. I attached it to the form and adjusted the setting and the gridview still does not auto refresh.
Dave Kreskowiak 19-Dec-17 15:41pm    
Your refresh code has to do a query to retrieve whatever it's displaying then rebind the grid to the data. You haven't shown the code that starts the Timer nor the code in the Timer Tick handler and the code the does the retrieve and rebind to the grid. Without that, nobody can tell you what's wrong.

Other methods will retire a large amount of work to implement, both in your app and on the SQL server side. I can't recommend anything because I don't know anything about your app, your requirements, the amount of data you're displaying, the amount of data you're working with, frequency of updates, your skill level, ... nothing.

For now, I'd say keep it simple if you've got a small number of clients.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900