I'm making a WPF project based on an Access database. The database has two tables:
- tblMovies (MovieID: PK, Title, Director, Genre etc)
- tblActors (ActorID: PK, MovieID: FK, Firstname, Lastname etc)
I have listbox where I can see all the movies, and if I click on one, it shows a new window with all the details about that movie: the title, director, genre, but also the actors.
In this window I have added a button to create a new actor. This opens a new window where you can enter the MovieID (FK) and the information about the actor. When I click save changes, it works and the window closes, but my listboxActors needs to be manually refreshed (I have added a button for that) to see the new actor.
Is there a way to refresh my listboxActors after I click "save changes" in my other window?
I first did it by closing my first screen when clicking add new actor, and then if I saved it would reopen the screen, and it'd automatically be refreshed, but I don't want it that way.
My listboxActors:
listBoxActors.ItemsSource = mov.Actors;
Save button (in the other screen)
private void buttonSaveNewActor_Click(object sender, RoutedEventArgs e)
{
Actor act = new Actor();
act.MovieID = Convert.ToInt32(textBoxMovieID.Text);
act.FirstName = textBoxFirstName.Text;
act.LastName = textBoxLastName.Text;
act.Country = textBoxCountry.Text;
act.Born = Convert.ToDateTime(BornDate.SelectedDate);
act.Bio = textBoxBio.Text;
ActorRepository.AddActor(act);
MessageBox.Show("The actor: " + act.FirstName + " " + act.LastName + " has been created");
this.Hide();
}
The refresh button:
private void buttonRefresh_Click(object sender, RoutedEventArgs e)
{
listBoxActors.ItemsSource = null;
listBoxActors.ItemsSource = mov.Actors;
}
Thanks in advance!
Well, Thanks for the explanatory comment...! I have a suggestion for you, please let me know if this helps you to code your scenario:
So you have two forms WindowShowDetails
Let it be the main-form and WindowAddActor
be the child, the Their will be a button in the main form which opens the child form, and you are doing some tasks in the child and press the Save button, which will save those details and closes that form. And you wanted to refresh the List in the main form associated with this event. For this you can use delegates and events;For this you have to do a number of tasks, in the main-form as well as in the child, Let me show you how it can help you:
Define a delegate in the main form:
public delegate void RefreshList();
Define an event of that delegate type
public event RefreshList RefreshListEvent;
Define a method that will do the action(ie, Refreshing the grid)
private void RefreshListView()
{
// Code to refresh the grid
}
Now need to define a Delegate in the WindowAddActor
Public Delegate UpdateActor;
Now we have to code the button click that opens the WindowAddActor
form Let the button be btnAddActor
so its Click event will be btnAddActor_click
, we have to initialize our delegate-event, the instance of the WindowAddActor
and assign the event to Delegate in the WindowAddActor
before showing that form. this can be coded as :
private void btnAddActor_click(object sender, EventArgs e)
{
WindowAddActor actorInstance = new WindowAddActor();
RefreshListEvent += new RefreshList(RefreshListView); // event initialization
actorInstance.UpdateActor = RefreshListEvent; // assigning event to the Delegate
actorInstance.Show();
}
Now we have to call the delegate from the SaveButton's click event that is
private void buttonSaveNewActor_Click(object sender, RoutedEventArgs e)
{
// Perform save operation
UpdateActor.DynamicInvoke(); this will call the `RefreshListView` method of mainWindow
this.Close();
}