1
votes

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!

4
I would look into RelayCommands and implementing INotifyPropertyChanged on your ViewModels and datagrid collectionDavid Lee
You're not switching between the forms - you're creating new ones each time. Are you wanting to simply switch or are you after the current behaviour you've got?Enigmativity
I just want to switch between Forms and then update the datagrid in Form1Ossi

4 Answers

1
votes

Ok - problem solved!

This wasn't working properly:

LagerDBEntities1 dataEntities = new LagerDBEntities1();          
var query =
   from product in dataEntities.Artikel
   select new { product.Id, product.artikelname, product.bestand };            
   dataGrid1.ItemsSource = query.ToList();

So i replaced it with this and it worked:

        SqlConnection con = new SqlConnection(@"Data Source=X");
        SqlDataAdapter Query = new SqlDataAdapter(@"SELECT Id, artikelname, bestand FROM Artikel", con);
        DataTable dt = new DataTable();

        Query.Fill(dt);

        dataGrid1.ItemsSource = null;
        dataGrid1.ItemsSource = dt.DefaultView;

Thanks to Binuriki Cliean Jay.

0
votes

Assuming your Form2 adds its information to the database, you could put your code that fills the Datagrid into a public method and then call it each time you load the form, because it looks like you create a new instance of Form1 after closing Form2.

public void LoadDatagrid()
{
    LagerDBEntities1 dataEntities = new LagerDBEntities1();          
    var query =
           from product in dataEntities.Artikel
           select new { product.Id, product.artikelname, product.bestand };            
           dataGrid1.ItemsSource = query.ToList();
}

and then call LoadDatagrid in Form1's Show event

0
votes

One solution would be to use the Window.Activated() event for Form 1

Just add the handler in the code for Form 1 and when the window is activated i.e. brought back to focus, the DataGrid is refreshed.

public class Form1 : Window {

    public Form1() {
        InitializeComponent();  
    }

    private void RefreshDataGrid() {
        // Refresh DataGrid Here
    }

    private void Form1_Activated(object sender, EventArgs e) {
        RefreshDataGrid();
    }
}

The only issue with this is that the event will be fired every time the window is activated.

Another way to achieve this is to pass a reference to Form 1 into Form 2 when you call it and trigger a public event in Form 1, from Form 2

public class Form1 : Window {

    public Form1() {
        InitializeComponent();
        Form2 f2 = new Form2(this);
    }

    public void RefreshDataGrid() {
        // Refresh DataGrid Here
    }
}

public class Form2 : Window {

    private Form1 Form1Ref;

    public Form2(Form1 f1) {
        Form1Ref = f1;
    }

    void Form2_Closing(object sender, CancelEventArgs e) {
        if (Form1Ref != null) Form1Ref.RefreshDataGrid();
    }
}

Apologies for any mistakes. Code was written freehand.

0
votes

in your win 1:

public MainWindow()
{
    InitializeComponent();
    LoadDatagrid();
}

public void LoadDatagrid()
{
    LagerDBEntities1 dataEntities = new LagerDBEntities1();          
    var query =
           from product in dataEntities.Artikel
           select new { product.Id, product.artikelname, product.bestand };            
           dataGrid1.ItemsSource = query.ToList();
}

change your codes when switching to win 2 into:

Window2 Auslagern = new Window2(currbestand, artikelid, artikelname);
Auslagern.Show();
this.Close();

then when you switch back from win 2 to win1:

MainWindow Main = new MainWindow();
Main.Show();
this.Close();

the public MainWindow() will take care of refreshing the datagrid