My main form, frmA, contains a dataGridView, dgvItems. When you click the
"Configure" menu item on frmA, a configuration form, frmB, is created and
shown. I wish to use frmB as the place where a user can customize the
dgvItems dataGridView (hide column, change column headings, etc.).
Problems (probably two systems of the same problem):
1) When coding in frmB, intellisense doesn't "know" dgvItems.
2) Then I tried this code istead: DataGridView dgv =
(DataGridView)this.Parent.Controls["dgvItems"];
When I try to execute this, I get a runtime error: "Object reference
not set to an instance of an object".
(I changed the declaration of dgvItems to public, and this did not
change the results)
Isn't it possible to access the controls of one form from another?
TIA
Phil
Rain County - 26 Jan 2006 05:51 GMT
I just found *a* solution to my own posted question. It was so simple, I
thought I'd share it in case anyone else would like to know.
I added a constructor to my frmB which expects a reference to the
dataGridView. When frmA creates and shows frmB, it passes the reference in
the constructor. The reference is then assigned to a privately declared
dataGridView IN frmB. Here's code snippets:
frmA -----------------------------------------
private void configureMenuItem_Click(object sender, EventArgs e)
{
frmB frm = new frmB(dgvItems);
frm.ShowDialog();
}
frmB ------------------------------------------
public partial class frmB : Form
{
DataGridView dgvItems;
//Default Constructor -- no longer neeeded; I will delete it
public frmB()
{
InitializeComponent();
}
//Custom Constructor
public frmB(DataGridView dgvItems)
{
this.dgvItems = dgvItems;
InitializeComponent();
}
Phil
> My main form, frmA, contains a dataGridView, dgvItems. When you click the
> "Configure" menu item on frmA, a configuration form, frmB, is created and
[quoted text clipped - 15 lines]
> TIA
> Phil