【问题标题】:C# - Binding FormClosing event to cause Form refreshC# - 绑定 FormClosing 事件以导致表单刷新
【发布时间】:2023-03-31 01:29:01
【问题描述】:

当另一个表单关闭时,我在刷新一个表单时遇到了一些困难。这是我到目前为止所拥有的,但它似乎并没有触发刷新。我对编程非常陌生,因此感谢您提供任何帮助!

private void button2_Click(object sender, EventArgs e)
{
    AddNewCourse ANCform = new AddNewCourse();
    ANCform.FormClosing += new FormClosingEventHandler(this.ANC_FormClosing);
    ANCform.Show();
}
private void ANC_FormClosing(object sender, FormClosingEventArgs e)
{
    this.Refresh();
}

【问题讨论】:

  • 如果你编码正确,这永远不需要任何帮助。停止使用 CreateGraphics(),它不是正确的代码。
  • 这是什么。刷新应该是“刷新”?
  • 它正在刷新原来的Form...我这里放的代码在Form 1中。按钮打开Form 2。当我关闭Form 2时,我需要Form 1来刷新。
  • 在表格1中说明需要使用刷新
  • this.refresh 不会重新绑定 gridview 的数据源。这可能就是为什么你没有得到你需要做的事情

标签: c# winforms event-handling formclosing


【解决方案1】:

ANC_FormClosing中重新绑定DataGridView的数据源

例如,如果我使用获取数据的方法重新绑定,我可能会编写

private void ANC_FormClosing(object sender, FormClosingEventArgs e)
{
    DataGridView.DataSource = GetFromDB();
}

这会使用来自数据库的新数据刷新网格

【讨论】:

  • 它将原始 Load 命令放入 FormClosing 事件处理程序中,这是 gridview 到两个数据库表的数据绑定。即:private void ANC_FormClosing(object sender, FormClosingEventArgs e) { Refresh(); this.courseTableAdapter.Fill(this.libraryDBDataSet1.Course); this.student_CourseTableAdapter.Fill(this.libraryDBDataSet.Student_Course); }
  • @DaveOli 调用this.Hide()this.Show() 似乎也起作用的原因不是Refresh() 方法,而是因为Show() 方法调用了on_load 事件。
  • 另外,我意识到在另一种形式中我使用的是 this.Hide 而不是 this.Close 所以它实际上并没有触发 FormClosing
  • 常见错误,我知道我早年曾经犯过这个错误
猜你喜欢
  • 1970-01-01
  • 2017-06-15
  • 2010-11-13
  • 1970-01-01
  • 2011-09-18
  • 1970-01-01
  • 2014-10-30
  • 2011-01-25
  • 1970-01-01
相关资源
最近更新 更多