You cannot "share" a UI control in multiple forms. A UI control is parented to the window that owns it. In your case that is your first form. Passing the DGV to another form and attempting to add it to that form isn't going to work. You should be passing the data between forms, not the UI controls.
In the child form you are accessing the grid that is contained in the parent form. While not recommended, it will work. However in order to update the grid the parent form must be processing messages. If you are showing the child form modally (ShowDialog
) then the parent form isn't responding to messages so don't expect the UI to change.
The reason you're not seeing the DGV on the child form at all (even if it did work) is because you have to add a control to the Controls
property of a form before it gets rendered. That is what the InitializeComponent
method does. It creates an instance of the controls you've defined in the designer and then adds them to the Controls
property of the form. In your constructor you then reassign the underlying field to a new control. That has no impact on the DGV that was added to the form and therefore the original (empty) DGV is getting shown in the form. Note that moving the assignment before the call to InitializeComponent
wouldn't resolve this either because that method will create a new instance of the DGV anyway.
As I already mentioned, you cannot share a UI control between forms. A control can only be rendered on 1 form. If you need the same data on multiple forms then pass the data between the forms. You can access a control on another form but this is considered to be bad design and not recommended. But that wouldn't help you here I believe.