Refresh Datagrid every 30 seconds

aloose

New member
Joined
Nov 2, 2012
Messages
2
Programming Experience
Beginner
I am at a total loss. I am far to new at this to understand how to do this. I have searched all over the net for this information with how to do it for days on end. I have a little program I made to pull data from a access database and display it on a screen for work, but I have no idea how to refresh it. I need it to refresh every 30 seconds or less. I read about timers and such. I do not understand how to set up a timer or where in the code it should be. I cannot believe its not an easy setting or option in the program. Why would someone want to make a datagrid with data thats not up to date.

Could someone please help me figure this out? I know it has to be so easy im overlooking it.

Thank you.
 
Firstly, it is easy. A Timer is about as simple a component as you can possibly get. Secondly, and I'm not having a go at you when I say this, as you get more experienced you will realise how silly that question about why you would not want to keep the grid up to date is. I have never created an application that required the data to be refreshed continually like that and you will likely encounter very few yourself. It may even be that this one doesn't require it and you're over doing it.

Anyway, with regards to the Timer, you would start by adding one to your form and setting its Interval property. If you want something to happen every 30 seconds then set the Interval to 30,000 (milliseconds). You then handle the Tick event. You create the event handler the same way you would a Click event handler for a Button. Again, just like a Button.Click event, you put the code you want executed in the event handler. You then call the Start and Stop methods of the Timer to do the obvious. Setting the Enabled property to True and False is equivalent.

The actual refreshing part, takes a bit of thought and planning. It's practical to simply throw away everything you've got and re-get everything from the database. For one thing, how will you ever edit a record if it disappears after 30 seconds? Also, it's very inefficient to keep getting the same data over and over. Ideally you would only be retrieving the data that has changed since you last retrieved it, but how are you going to determine which records have changed and which haven't?

That's where the planning comes in. You need to add a column to your database table(s) to contain the time of the last modification. You also can't ever delete any records. If records need to be removed then you need a Boolean column to indicate that and logically deleting a record entails flipping that flag and setting the modified time. When you retrieve the data for the first time you will get all the data and then each subsequent query will only retrieve the data for which the modified time is later than the latest value in your current data. Because you're not physically deleting any records, you will also be retrieving those records that have been logically deleted, so you can filter them out at the UI level. You also don't need to discard the data you've already got. You can configure your data adapter the way you want to handle records that have changed.
 
I have no need to do anything but show data from the access database. I have a query inside Access that pre filters the results so by the time they get to the program there should only be a few boxes of data. I would like to go basic to start where I just update and read the database again and repopulate the datagrid. The grid is completely locked down so its view only, no way to modify or delete anything. I see a timer but the only thing I can see is a Tag field besides the interval.

I didnt know about events. This opens a whole new world of exploration :) Thank you. I have it refreshing now. Where do I add new Events BTW? I only have 3 on the list
 
You don't add new events. The Timer works the way it works. If you create your own classes then you can add your own events and add your own code to raise them appropriately. If you want learn about creating your own events in your own classes, follow the Blog link in my signature and check out my post on Custom Events.

I have to say though, it concerns me that you're trying to create an application that works with a database without knowing about something as fundamental as handling events. I would really recommend that you find yourself a good beginner tutorial and work your way through it systematically. This is a good example:

Microsoft Visual Basic .NET tutorials for Beginners
 
Back
Top