簡體   English   中英

如何在運行時重新加載數據網格視圖 c#

[英]How to reload the data grid view during runtime c#

每次打開form時,我都會重新加載data grid view的數據。

 Student_DetailEntities db = new Student_DetailEntities();
 private void Form1_Load(object sender, EventArgs e)
 {
     db.StudentTables.Load();
     studentTableBindingSource.DataSource = db.StudentTables.Local;
 }

為確保data table refreshshowdata grid view中,我嘗試了此代碼

private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
{
        Application.Restart();
        Environment.Exit(0);
}

我嘗試

private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
{
        this.Controls.Clear();
        this.InitializeComponent();
}
  

但是data grid view仍然not reload 每次additem時,我都需要close並再次open form ,以便它顯示在data grid view中。

您可以使用Shown事件而不是Form.Load 。這樣您就可以在每次顯示表單時重新加載數據。

Student_DetailEntities db = new Student_DetailEntities();
private void Form1_Load(object sender, EventArgs e)
{
    Shown += Form1_Shown; 
}
 
 
private void Form1_Shown(object sender, EventArgs e) 
{
     db.StudentTables.Load();
     studentTableBindingSource.DataSource = db.StudentTables.Local;
}

請參閱Form.LoadForm.Shown的詳細信息:

Windows Forms 中的事件“Form.Load”、“Form.Shown”和“Form.Activated”的順序

@Caius Jard在評論中指出,您可以通過“Form Designer --> Properties --> Events --> 雙擊 Shown 然后添加代碼”而不是Shown += Form1_Shown;來簡單地添加Form.Shown事件處理程序。 在我上面提到的Form1_Load中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM