I want to refresh my datagrid in Form1 after I updated the database in Form2. My Problem is that it isn't updating the datagrid in Form1 after i close Form2. Literally I am not sure how to do that. Can someone help me please?
This is how i create my datagrid (in Form1):
LagerDBEntities1 dataEntities = new LagerDBEntities1();
var query =
from product in dataEntities.Artikel
select new { product.Id, product.artikelname, product.bestand };
dataGrid1.ItemsSource = query.ToList();
This is how i switch to Form2:
Window2 Auslagern = new Window2(currbestand, artikelid, artikelname);
this.Close();
Auslagern.Show();
This is how i switch to Form1:
MainWindow Main = new MainWindow();
this.Close();
Main.Show();
In Form2 I am updating the database:
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();
}
So how can I switch back to Form1 and see the updated datagrid?
Thanks in advance!
RelayCommands
and implementingINotifyPropertyChanged
on yourViewModels
and datagrid collection – David Lee