Execute a function on another, already open, form
I have a form with datagridview inside it.
When you double click on a row from the datagridview, another form will open, which is basically a form where you can edit the data that you just double clicked on.
This "edit" form has 3 buttons, "Delete", "Refresh" and "Return to Main Form".
When you complete what you were supposed to do on this form, it closes.
My question is:
When this form is closed, I want the data that is inside the datagridview in the main form to be updated, how can I call this function on the main form from the edit form.
Keep in mind that I already have a reload function, let's call it refreshData () ;.
source to share
If you've used .ShowDialog (), just install the update function on this line of code.
The program will continue to work with
private void cell1_DoubleClick(object sender, System.EventArgs e)
function.
So your code will look like this;
private void cell1_DoubleClick(object sender, System.EventArgs e)
{
//Your previous code ....
//The part where you open the EditForm
MyEditForm.ShowDialog();
//After it has been closed the program will continue to execute this function(if it has not been ended yet)
RefreshData();
//Since this function is running from your main form, the function RefreshData() will be executed on your main form aswell
}
You don't need to check some of the dialogue results.
source to share
if you open the edit form as a modal, the ShowDialog () call is blocked, so if you place a call to refreshData after that, it will be executed after the edit form is closed:
var editForm = new EditForm(...);
var result = editForm.ShowDialog();
if (result == DialogResult.OK)
{
refreshData();
}
source to share
I think this will work:
Add a DatagridviewForm property of type DatagridviewForm (you probably have a different name / type) to AnotherForm. In the part where you call anotherForm.ShowDialog add the following code:
anotherForm = new AnotherForm();
anotherForm.DatagridviewForm = this;
anotherForm.ShowDialog();
anotherForm.Dispose();
In AnotherForm's close handler, update or update the data:
private void AnotherForm_FormClosed(object sender, FormClosedEventArgs e)
{
DatagridviewForm.refreshData();
}
source to share
You can access the data when you close the form
Form MyEditForm;
private void cell1_DoubleClick(object sender, System.EventArgs e)
{
if (MyEditForm==null)
{
MyEditForm=new MyEditForm();
MyEditForm.FormClosing += refreshData;
}
MyEditForm.ShowDialog();
}
private void refreshData(object sender, EventArgs e)
{
var myDataObj=MyEditForm.getData();
}
source to share