0

I'm a beginner in C#. I have a DataGridvew "dgPins" defined in a class along with a Panel named "pnlPins". The application is designed using several UIs combined together. The initializing is done in class X as follows

private void InitializeComponent() {
    ...
    private System.Windows.Forms.Panel pnlPINs;
    private System.Windows.Forms.DataGridView dgPins;
    ...
}

I use this grid to display some pins read from a device connected to Ethernet. Once the data is filled it looks like this(this is done with a button click),

private void mnuGeraetLesen_Click(object sender, EventArgs e)
{
    ...
    if (EthernetAvailable == true)
    {
         QueryAllCodeCommandHandler();// local function which fills the grid
    }
}

enter image description here

Now in another UI(class) I have a button which needs to access the same grid instance and do the same task. So I have done the following in that class(let's say class Y).

private void _btnSearchDevices_Click(object sender, EventArgs e)
{
    SingleDeviceInformationControl test = new SingleDeviceInformationControl();
    test.QueryAllCodeCommandHandler();
}

But as I understand this will create a different instance and will not work. So the grid remains empty as below.

enter image description here

I thought of renaming the dataGridview to public static which enables me to access the same instance from anywhere but it makes the whole project unstable. I have done some research and found similar questions but haven't understood what I really have to do to achieve the expected result. I really appreciate any hints for a possible solution for this issue. Thank you very much!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Isuru
  • 430
  • 5
  • 21
  • Since you declared your DataGridView to be private you can only access it from the same class, not from anywhere else. So either you need to declare it as public or you need to write setter and getter methods to access the DataGridView (see [What is the difference between public, private, protected and nothing](https://stackoverflow.com/questions/614818/what-is-the-difference-between-public-private-protected-and-nothing) for reference). I also don't understand what you mean by your project becoming unstable by declareing the grid view as public? – waka Sep 21 '17 at 09:15
  • sorry my bad. It doesn't change anything. Because I forgot to mention(m bad) that those are separate UIs – Isuru Sep 21 '17 at 10:04

1 Answers1

0

If you are instantiating YourForm in an other form you are obliged to make your Grid Public to access it or encapsulate the private Grid in a 'public' property .

If you are talking about separate UIs or UserContorls You can create a static Instance of your UI ,like this you are obliged to modify your Grid from private to public . But it recommended that you encapsulate the private Grid in a 'public' property

private static YourForm _instance;

/// <summary>
/// static Instance of YourForm 
/// </summary>
public static YourForm Instance
{
  get
  {
    if (_instance == null)
      _instance = new YourForm();
    return _instance;
  }
}

YourForm is the form that contain your grid and then you can access to your Grid like this

YourForm.Instance.dgPins

this approach is used generally with UserControls

Ahd Bk
  • 139
  • 5