KayleighArianna KayleighArianna - 3 months ago 11
C# Question

From a UserControl within a Form, How can I access the DataGridView of the parent Form?

Problem: I am trying to access the DataGridView of my Windows Form (

ClientSearch.cs
) from a User Control (
AttachmentOptions.cs
) so that I can use the data retrieved by it.

Background:
ClientSearch.cs
is a windows form that can be brought up on an email on my Outlook add-in. It allows the user to search for the client (and their respective folder on the system) to archive the email or attachments to.
AttachmentOptions.cs
is a User Control that is generated for each attachment in the email so that each attachment will have the same group of archiving options.




I have taken a few steps to sorting out this problem and I keep running into more issues. In case I have made the situation worse, I will show my steps:

Original:

var txtClient = parentItem.Controls.Find("txtClient", true); //This works as I want
var dgvClients = parentItem.Controls.Find("dgvClients", true);
DataGridView test = dgvClients; //Error appears on this line
strClientName = test.CurrentRow.Cells[0].Value.ToString();


This is the error:


Error 1 Cannot implicitly convert type 'System.Windows.Forms.Control[]' to 'System.Windows.Forms.DataGridView'





Fix Attempt 1:

var txtClient = parentItem.Controls.Find("txtClient", true);
var dgvClients = (DataGridView)parentItem.Controls.Find("dgvClients", true).Cast<DataGridView>; //Error on this line
DataGridView test = dgvClients;
strClientName = test.CurrentRow.Cells[0].Value.ToString();


Error:


Error 20 Cannot convert method group 'Cast' to non-delegate type 'System.Windows.Forms.DataGridView'. Did you intend to invoke the method?





Fix Attempt 2:

var dataGridInfo = parentItem.Controls.Find("dgvClients", true);
var dgvClients = (DataGridView)dataGridInfo; //Error is here
DataGridView test = dgvClients;
strClientName = test.CurrentRow.Cells[0].Value.ToString();


Error:


Error 20 Cannot convert type 'System.Windows.Forms.Control[]' to 'System.Windows.Forms.DataGridView'





Fix Attempt 3:

var dataGridInfo = parentItem.Controls.Find("dgvClients", true);
DataGridView dgvClients = dataGridInfo.Cast<DataGridView>; //Error is here
DataGridView test = dgvClients;
strClientName = test.CurrentRow.Cells[0].Value.ToString();


Error:


Error 20 Cannot convert method group 'Cast' to non-delegate type 'System.Windows.Forms.DataGridView'. Did you intend to invoke the method?





Fix Attempt 4:

//var dgvClients = parentItem.Controls.Find("dgvClients", true);
var dataGridInfo = parentItem.Controls.Find("dgvClients", true);
DataGridView dgvClients;
dgvClients = (DataGridView)dataGridInfo; //Error is here
DataGridView test = dgvClients;
strClientName = test.CurrentRow.Cells[0].Value.ToString();


Error:


Error 20 Cannot convert type 'System.Windows.Forms.Control[]' to 'System.Windows.Forms.DataGridView'





I feel like I am going round in circles and worsening my code as I go (hence why I showed previous versions) in trying to talk to the DataGridView. It seems that c# does not like converting these two types together so is there another way to refer to a DataGridView object?

For reference
parentItem
refers to a previously declared
ClientSearch
and this works for talking with
ClientSearch.cs
with things like textboxes.

Many thanks in advance for your time and help

Answer

The error is right there, staring you in the face.

Go back to your original code. You are getting an array back from Controls.Find(), and are trying to treat it as a single object. You probably want the first item in that array, assuming you know there's only one of those.

var txtClient = parentItem.Controls.Find("txtClient", true);
var dgvClients = parentItem.Controls.Find("dgvClients", true);
DataGridView test = dgvClients[0] as DataGridView;
strClientName = test.CurrentRow.Cells[0].Value.ToString();