【发布时间】:2017-10-26 23:07:57
【问题描述】:
在 Form2 中更新数据库后,我想刷新 Form1 中的数据网格。我的问题是在我关闭 Form2 后它没有更新 Form1 中的数据网格。从字面上看,我不知道该怎么做。有人可以帮帮我吗?
这就是我创建数据网格的方式(在 Form1 中):
LagerDBEntities1 dataEntities = new LagerDBEntities1();
var query =
from product in dataEntities.Artikel
select new { product.Id, product.artikelname, product.bestand };
dataGrid1.ItemsSource = query.ToList();
这就是我切换到 Form2 的方式:
Window2 Auslagern = new Window2(currbestand, artikelid, artikelname);
this.Close();
Auslagern.Show();
这就是我切换到 Form1 的方式:
MainWindow Main = new MainWindow();
this.Close();
Main.Show();
在 Form2 中我正在更新数据库:
int counter = currentbestand - ValueS;
if (counter > 0)
{
SqlConnection con = new SqlConnection(@"X");
try
{
con.Open();
string Query = "update Artikel set bestand='" + counter + "' where id='" + artikelid + "' ";
SqlCommand createCommand = new SqlCommand(Query, con);
createCommand.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
MainWindow Main = new MainWindow();
this.Close();
Main.Show();
}
那么如何切换回 Form1 并查看更新后的数据网格?
提前致谢!
【问题讨论】:
-
我会调查
RelayCommands并在您的ViewModels和datagrid 集合上实施INotifyPropertyChanged -
您没有在表单之间切换 - 您每次都在创建新表单。您是想简单地切换还是在您当前的行为之后?
-
我只是想在Forms之间切换然后更新Form1中的datagrid
标签: c# wpf visual-studio