views:

50

answers:

3

HI,

Iam trying to get value's out of a datagridview. this datagridview is on form one.

and where i want the value's is on form two;

but i dont want to do this :

[code]form1 frm = new from1();[/code]

because that form1 already exists so i dont want to create it again

can anytone plz help me get a solution for this thank you very much

+1  A: 

You can access other open forms using the OpenForms collection on Application:

Application.OpenForms

Then all you need to do is test for the type or name of the form and cast it to your second form to grab the reference, then you can access its properties etc.

However, grabbing pieces of information like this across forms is considered bad design. If the information can be aggregated out into something both forms can reference, this is better. Alternatively, if the forms need to interact based on the state of each of their data, consider creating events between the two forms.

Adam
if i do this: <code>frmFactOverzicht frm = Application.OpenForms["frmFactOverzicht"];</code>then it gives me this error Error 4 Cannot implicitly convert type 'System.Windows.Forms.Form' to 'WindowsFormsApplication1.frmFactOverzicht'. An explicit conversion exists (are you missing a cast?) C:\Users\FB\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\InkFacturen\frmEditFact.cs 156 36 WindowsFormsApplication1can you please help me with that?
Willem
You need to explicitly cast it: frmFactOverzicht frm = (frmFactOverzicht)Application.OpenForms["frmFactOverzicht"];
Adam
+1  A: 

Please don't even try to do that. Store your data in a data container object that is shared between the two forms. Bind form1 to the data and access it from form2.

AZ
I'll assume the -1 is you, there is no need for that - another interpretation of the question is how to access existing forms when you weren't given a reference. I never said it was the best way to do it, nor did I advise it.
Adam
sorry my first time here on the forum
Willem
but can you please see my other post here in the topic
Willem
@willem I've answered the question you asked in the comments with another comment.
Adam
@adam thank you very much it is al working right now!
Willem
A: 

if id do this:

frmFactOverzicht frm = Application.OpenForms["frmFactOverzicht"];

then it gives me this error

Error 4 Cannot implicitly convert type 'System.Windows.Forms.Form' to 'WindowsFormsApplication1.frmFactOverzicht'. An explicit conversion exists (are you missing a cast?) C:\Users\FB\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\InkFacturen\frmEditFact.cs 156 36 WindowsFormsApplication1

can you please help me with that?

Willem