简体   繁体   English

窗体刷新问题Windows窗体ASP.net

[英]Form refresh issue windows form ASP.net

I am trying to refresh a form after every 30 minutes if the datagrid is empty. 如果datagrid为空,我尝试每30分钟刷新一次表单。 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 . 当datagrid的RowCount为<=0时,此代码效果很好,但即使datagrid包含的行> 0该代码仍将继续。 How can I prevent refreshTimer_Tick if the datagrid contains rows? 如果数据网格包含行,如何防止refreshTimer_Tick?

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(); 现在,只需调用refreshTimer.Stop(); when needed in another method. 当需要另一种方法时。

PS asp.net and winforms are not the same thing. PS asp.netwinforms不是一回事。

You call the if statement just after the databind methode. 您可以在databind方法之后调用if语句。 And I think it executes before your datagrid binds its rows. 而且我认为它在您的数据网格绑定其行之前执行。

For me you should remove this code from your refreshTimerTick event : 对我来说,您应该从refreshTimerTick事件中删除以下代码:

    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 并将其放在DataBount事件中

  void dataGrid_DataBound(object sender, EventArgs e)
  {
     if (dataGrid_FileList.RowCount>0)
        {
            InhouseDownloadeer_Shown(this, null);
        }    
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM