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.


VulpesRank #55 · 29.2K reputationPosted Apr 14, 2013, 3:56 PM EDT
Violeta Popa132 reputationPosted Apr 15, 2013, 12:49 AM EDT
Riddhi ValechaRank #794 · 2.9K reputationPosted Apr 15, 2013, 5:01 AM EDT
check the condition -
if(calculPolita == null)
{
calculPolita = new CalculPolita(); // classname
}
else
{
}
--------
Hope this helps
VulpesRank #55 · 29.2K reputationPosted Apr 15, 2013, 6:09 AM EDT
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;