I am trying to refresh a form after every 30 minutes if the datagrid is empty. My code is as below:
private void Form1_Load(object sender, EventArgs e)
{
BindDataGrid();
if (dataGrid_FileList.RowCount <=0)
{
Timer refreshTimer = new Timer();
refreshTimer.Interval = 30000; //30 seconds in milliseconds
refreshTimer.Tick += new EventHandler(refreshTimer_Tick);
refreshTimer.Start();
}
}
void refreshTimer_Tick(object sender, EventArgs e)
{
this.Controls.Clear();
this.InitializeComponent();
BindDataGrid();
if (dataGrid_FileList.RowCount>0)
{
InhouseDownloadeer_Shown(this, null);
}
}
This code works well when RowCount of datagrid is <=0
but it's continuing even after the datagrid contains rows > 0
. How can I prevent refreshTimer_Tick if the datagrid contains rows?
You can also stop a Timer. Declare the timer variable outside the method.
Timer refreshTimer = new Timer();
private void Form1_Load(object sender, EventArgs e)
{
refreshTimer.Interval = 30000;
refreshTimer.Tick += new EventHandler(refreshTimer_Tick);
}
Now simply call refreshTimer.Stop();
when needed in another method.
PS asp.net
and winforms
are not the same thing.
You call the if statement just after the databind methode. And I think it executes before your datagrid binds its rows.
For me you should remove this code from your refreshTimerTick event :
if (dataGrid_FileList.RowCount>0)
{
InhouseDownloadeer_Shown(this, null);
}
Like :
void refreshTimer_Tick(object sender, EventArgs e)
{
this.Controls.Clear();
this.InitializeComponent();
BindDataGrid();
}
And put it in the DataBount event
void dataGrid_DataBound(object sender, EventArgs e)
{
if (dataGrid_FileList.RowCount>0)
{
InhouseDownloadeer_Shown(this, null);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.