9.14. Refreshing a DataSet Automatically Using Extended Properties
Problem
You need to automatically refresh a DataSet
periodically.
Solution
Use extended properties and a timer.
The sample code contains two event handlers and one method:
Form.LoadSets up the sample by creating a
DataTablecontaining the Categories table from the Northwind database. The default view of the table is bound to a data grid on the form. A secondDataTablewith the auto-refreshing functionality is created that also contains the Categories table from the Northwind database. The default view of the auto-refreshing table is bound to a second data grid on the form. An extended propertyRefreshTimeis added to the auto-refreshing table and set to 15 seconds—the value of the constantDATAREFRESH_SECONDSin the sample—into the future. Finally, a thread timer is created with aTimerClassbackdelegateCheckRefreshDataSet, with a due time of one second, and a period of one second.- Update
Button.Click Uses a
DataAdapterto update changes made to the firstDataTableback to the data source.CheckRefreshDataSet( )This method is called periodically by the thread timer. The method checks whether the current time is later than the time in the
RefreshTimeextended property of the auto-refreshing table. If it is, aDataAdapteris used to fill the table with the latest data from the data source and theRefreshTimeextended property is once again set to 15 seconds into the future.
The C# code is shown in Example 9-17.
Example 9-17. File: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access