简体   繁体   English

单击按钮后刷新表格

[英]refresh form after button click

I'm getting the first element of a table articles where column(statusArticle=false).The problem is that i want to refresh the form after the button click so i can interact with next element, but the form is not refreshed, i tried several codes exept the one of application.restart that is heavy! 我正在获取表格文章的第一个元素所在的列column(statusArticle = false)。问题是我想在单击按钮后刷新表单,以便可以与下一个元素进行交互,但是表单没有刷新,我尝试过有几条代码是application.restart的繁重代码之一! How can i refresh the form at the boutton click without restarting application? 我如何在单击按钮时刷新表单,而无需重新启动应用程序?

   private void button_Click(object sender, EventArgs e)
        {
            using (DbEntities db = new DbEntities())
            {
               Articles firstArticle = db.Articles.FirstOrDefault(u => u.statusArticle == false);
                if (firstArticle != null)
                {
                    firstArticle.statusArticle = true;
                    db.SaveChanges();
                    MessageBox.Show("Article validated", "OK");
                    this.Refresh();
                }
            }
        }

Within your class create the following: 在您的课程中创建以下内容:

private void ShowArticle(Article article)
{
  /* The code currently in your constructor for displaying the 
     first article goes here */
}

For your constructor: 对于您的构造函数:

public MyForm()
{
  using(DbEntities db = new DbEntities())
  {
    Articles firstArticle = db.Articles.FirstOrDefault(u => u.statusArticle == false);
    if( firstArticle != null ) ShowArticle( firstArticle );
  }
}

In your button click handler instead of calling Refresh simple replace it with a call to ShowArticle passing in "firstArticle". 在您的按钮单击处理程序中,而不是将其简单地调用Refresh,而是将其替换为传递给firstArticle的ShowArticle的调用。 The code above could be cleaned up a bit, but it should do. 上面的代码可以清除一点,但是应该这样做。

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

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