I have this code:
calculPolita calculCasco = (calculPolita)Application.OpenForms["calculPolita"];
DataGridView dgv = calculCasco.calculGridView;// i get the error 'Object reference not set to an instance of an object.' Why? thank you.

VulpesPosted Apr 15, 2013, 10:09 AM
If it's running from the form which launched the child form containing the DGV, then you need to store a reference to the child form in a field of the parent form so that you can access it later:-
private calculPolita calculCasco;
// in button click handler
calculCasco = new calculPolita();
calculCasco.MdiParent = this; // or whatever you're doing here
calculCasco.Show();
// in some other method of parent form
DataGridView dgv = calculCasco.calculGridView;
If it's running from some other form, then probably the easiest thing to do is to introduce an internal static field into the child form class which stores a reference to it:
class calculPolita : Form
{
internal static calculPolita Me;
public calculPolita() // constructor
{
InitializeComponent();
Me = this;
}
// rest of code
}
Another form can then access the DGV as follows:
DataGridView dgv = calculPolita.Me.calculGridView;
Riddhi ValechaPosted Apr 15, 2013, 9:01 AM
check the condition -
if(calculPolita == null)
{
calculPolita = new CalculPolita(); // classname
}
else
{
}
--------
Hope this helps
Violeta PopaPosted Apr 15, 2013, 4:49 AM
VulpesPosted Apr 14, 2013, 7:56 PM