[英]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
refresh
並show
在data 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
。 每次add
新item
時,我都需要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.Load
、 Form.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.