Situation
Form1
with two User Controls (called startControl
and resultsControl
) addedresultsControl
has a DataGridView
called dataGridView1
which has been added by the DesignerstartControl
has a DataTable
called dt
Goal
DataTable
from startControl
to my DataGridView
in resultsControl
What I've tried is to expose my dataGridView1
from resultsControl
as a property and access it's DataSource
in startControl
but for some reason the exposed property isn't visible to me in neither Form1
nor startControl
.
Code
public partial class resultsControl : UserControl
{
public resultsControl()
{
InitializeComponent();
}
[PropertyTab("Data"), Description("Test"), Category("Misc"), Browsable(true)]
public DataGridView dgvParameter
{
get
{
return this.dataGridView1;
}
set
{
this.dataGridView1 = value;
}
}
}
I can't seem to access my dataGridView1
by using resultsControl.dgvParameter
- what am I doing wrong?
EDIT
As pointed out I need to work with an instance of resultsControl
. I already created an instance of resultsControl
in my Form1
- that means I just need to access it from my startControl
user control, right?
My first guess would've been to expose another property in Form1
or is there another way to access it from my (parent) form?
Code
public partial class homeForm : Form
{
public homeForm()
{
InitializeComponent();
}
private void btnDashStart_Click(object sender, EventArgs e)
{
startControl control = new startControl();
ShowControl(control);
}
private void btnDashResults_Click(object sender, EventArgs e)
{
resultsControl control = new resultsControl();
ShowControl(control);
}
public void ShowControl (Control control)
{
containerPanel.Controls.Clear();
control.Dock = DockStyle.Fill;
control.BringToFront();
control.Focus();
containerPanel.Controls.Add(control);
}
}
You need to use the instance of resultsControl
because your dgvParameter
is not a static
property.
Go into the designer view and click on your control. In the "Properties" window, look at the 'Name' property. That's the name of your instance, e.g. resultsControl1
. Now you can use resultsControl1.dgvParameter
in your code.
By the way, you should adhere to naming conventions, i.e. properties and classes should be named using UpperCamelCase.