0

I have a method in a page public partial class pagThreads : Page for data loading from DB to DataGrid:

public void dbReadData()
{
    DbAccess db = new DbAccess();
    lista = db.GetThreads();
    dgrThreads.ItemsSource = lista;
}

To edit those data I'm opening another window like this (by double clicking on DataGrid row):

private void dgrThreads_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    wndSabThrEdit wndSabThrEdit = new wndSabThrEdit();
    wndSabThrEdit.ShowDialog();
}

After editing I can save the data by updating the record in DB like this:

public void SaveSettings()
{
    DbAccess dbAccess = new();
    dbAccess.UpdateSabThreads();
    string message = "Settings saved.";
    string title = "Information";
    MessageBoxResult result = System.Windows.MessageBox.Show(message, title, MessageBoxButton.OK, MessageBoxImage.Information);
    if(result == MessageBoxResult.OK)
    {
        Close();    
    }
}

The question is: How to trigger the method dbReadData() again to refresh the data in the the DataGrid, for example after closing the editing window? I will be gratefull for help. Thank you!

5
  • Can't you just call dbReadData after wndSabThrEdit.ShowDialog();? Commented Apr 14, 2022 at 11:16
  • Hi Klaus, it will be before editing and saving the data in DB. I was wondering to make it somewhere here: if(result == MessageBoxResult.OK) { Close(); } but in this window i can't access dbReadData() method Commented Apr 14, 2022 at 11:26
  • Is SaveSettings a method in the Window that's opened'? Commented Apr 14, 2022 at 11:34
  • page pagThreads owns DataGrid and dbReadData() and window wndSabThrEdit owns SaveSettings() method Commented Apr 14, 2022 at 11:46
  • Your first suggestion Klaus Gutter was good, thank you! I didn't know that the code stops in ShowDialog().. Commented Apr 14, 2022 at 12:04

2 Answers 2

2

From your comment it looks like you think that putting

dbReadData();

after

wndSabThrEdit.ShowDialog(); 

would run immediately.

It won't.

ShowDialog() blocks, and the code will only continue to dbReadData(); after the Window has closed.

So just change to:

private void dgrThreads_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    wndSabThrEdit wndSabThrEdit = new wndSabThrEdit();
    wndSabThrEdit.ShowDialog();
    SaveSettings();
}
Sign up to request clarification or add additional context in comments.

1 Comment

You're absolutely right. It works like it should!!! Have a good day!
0

If wndSabThrEdit inherits from Window it has a closing event that you can subscribe to:

wndSabThrEdit.Closing += (_,_) => dbReadData();
wndSabThrEdit.ShowDialog();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.