从窗体中的 UserControl,如何访问父窗体的 DataGridView?

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

问题: 我正在尝试从用户控件 (AttachmentOptions.cs) 访问我的 Windows 表单 (ClientSearch.cs) 的 DataGridView这样我就可以使用它检索到的数据。

背景: ClientSearch.cs 是一个 windows 表单,可以在我的 Outlook 加载项的电子邮件中显示。它允许用户搜索客户端(以及他们在系统上各自的文件夹)以将电子邮件或附件存档​​到其中。 AttachmentOptions.cs 是为电子邮件中的每个附件生成的用户控件,以便每个附件都具有相同的归档选项组。


我已经采取了一些步骤来解决这个问题,并且我会 运行 处理更多问题。如果我让情况变得更糟,我会展示我的步骤:

原文:

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();

这是错误:

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


修复尝试 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 20 Cannot convert method group 'Cast' to non-delegate type 'System.Windows.Forms.DataGridView'. Did you intend to invoke the method?


修复尝试 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 20 Cannot convert type 'System.Windows.Forms.Control[]' to 'System.Windows.Forms.DataGridView'


修复尝试 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 20 Cannot convert method group 'Cast' to non-delegate type 'System.Windows.Forms.DataGridView'. Did you intend to invoke the method?


修复尝试 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 20 Cannot convert type 'System.Windows.Forms.Control[]' to 'System.Windows.Forms.DataGridView'


在尝试与 DataGridView 对话时,我感觉自己在原地打转,而且我的代码在不断恶化(因此我展示了以前的版本)。似乎 c# 不喜欢将这两种类型一起转换,那么是否有另一种方法来引用 DataGridView 对象?

作为参考,parentItem 指的是之前声明的 ClientSearch,这适用于与 ClientSearch.cs 进行文本框之类的对话。

非常感谢您的宝贵时间和帮助

所以你有多个问题,让我们来解决它们:

  1. 查找returns控件数组:

要解决这个问题,您需要从中取出第一项:

var dgvClients = parentItem.Controls.Find("dgvClients", true)
                                    .FirstOrDefault() as DataGridView;
  1. Cast是一种方法:

调用方法时,需要使用括号():

.Cast<DataGridView>();

错误就在那里,直视着你。

回到原来的代码。您正在从 Controls.Find() 获取一个数组,并试图将其视为单个对象。您可能想要该数组中的第一项,假设您知道只有其中一项。

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();

UserControl 的父窗体中按名称查找控件确实是个坏主意。

相反,您应该为您的 UserControl 创建一个 DataGridView 类型的 属性 并使用代码或设计器分配它并在您的 UserControl 中使用它。

public DataGridView ClientsGrid { get; set; }

您不依赖命名约定,而是依赖开发人员的选择。