Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form Methods

Status
Not open for further replies.

danielkelly

IS-IT--Management
Aug 13, 2006
28
AU
I have an application that shows a datagrid and when i double click on a cell, it opens a new form with the details of the selected row so i can edit the values.

The question i have is how can I tell the main datagridview to refresh once i save the record.

I am populating the datagridview from a method which connects to the database and updates the datagrid. So basically i want to call that method but from the new Editing form. Is this possible??

Thanks in advance.

 
Yes this is possible, to call the method in your first form, from the editing form, just call the parent form of the editing form, cast as the type of your main form. Say for example your main form is frmMain, in your editing form you would say:

((frmMain)this.ParentForm).MyMethod(MyParameters);

 
Or you can create a delegate (called something like DataChangedEventHandler), and raise that event in the child form.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Or you can use a design pattern like model-view-controller and the observer pattern that can handle that kind of issue for you.
 
Thanks for the replies but im still having problems getting it to work. Here is a breakdown of what i have.

My main form is called frmStatus and on that form i have a datagridview (dgvStatus). I have assigned an event that handles the double click of the datagridview. Behind that event i have the code :-

frmEdit form1 = new frmEdit("strNumber");
frmEdit.ShowDialog();

I have modified the constructor of the Edit Form to accept a string which is the number of the record. From that number I connect to the database and populate the fields on the form.

After I have saved the record, i execute the following as per teh above post :-

((frmStatus)this.ParentForm).UpdateStatus();

However im getting an error. Something along teh lines of Object not an instance. When i debug, I see that the ParentForm attribute is saying it is null..

Any ideas.



 
in your frmEdit add the following line near your variables

public event EventHandler UpdateStatus;

then modify the calling code to:

frmEdit form1 = new frmEdit("strNumber");
form1.UpdateStatus = new EventHandler(form1_UpdateStatus);
frmEdit.ShowDialog();


and create a new method below the chunk of code where you call this. The code should be:

private void form1_UpdateStatus(object Sender, EventArgs e)
{
// do your updating here
}

and finally, where you call

((frmStatus)this.ParentForm).UpdateStatus();

delete that line and add
this.UpdateStatus(this, EventArgs.Empty);

and your code should run. This is the suggestion made by Chip about adding your own delegate using an existing event handler.
 
I have followed the above code but have still yet to get it working. Here is my code sample :-

frmPCRStatus = Main Form
frmEdit = Editing Form

In the frmEdit I have at the top

public event EventHandler UpdateStatus;

In the Parent (calling form frmPCRStatus) I have the following :-

frmEditPCR formEdit = new frmEditPCR("strNumber");
formEdit.UpdateStatus = new EventHandler(formEdit_UpdateStatus);
formEdit.ShowDialog();

private void formEdit_UpdateStatus(object Sender, EventArgs e)
{
UpdatePCRs();
}

On the actual Editing form I have the following at the end :-

this.UpdateStatus(this,EventArgs.Empty);

However when i try to run this code i get the following error :-

Error 1 The event 'Jtech_Service_Central.frmEditPCR.UpdateStatus' can only appear on the left hand side of += or -= (except when used from within the type 'Jtech_Service_Central.frmEditPCR')

Can any1 provide some assistance why this is happening.

Thanks.
 
formEdit.UpdateStatus += new EventHandler(formEdit_UpdateStatus);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top