2016-08-12 172 views
1

问题:我想从用户控件(AttachmentOptions.cs)访问我的Windows窗体(ClientSearch.cs)的DataGridView,以便我可以使用它检索的数据。从窗体内的用户控件,我如何访问父窗体的DataGridView?

背景:ClientSearch.cs是一个窗体,可以在我的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交谈。

提前为您的时间非常感谢,并帮助

+2

在UserControl的父窗体中找到一个名称控件真是一个坏主意。相反,您应该为'UserControl'创建'DataGridView'类型的属性,并使用代码或设计器将其分配,并在'UserControl'中使用它。 –

回答

2

的错误就在那里,盯着你的脸。

回到您的原始代码。您正在从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(); 
0

所以,你有多个问题,让我们解决这些问题:

  1. 查找返回控件数组:

要解决的是,你需要抓住第一个项目:

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

调用方法时,您需要使用paranthesis ()

.Cast<DataGridView>(); 
1

这真是一个糟糕的主意,以查找UserControl的父窗体通过名称来控制。

相反,您应该为UserControl创建一个DataGridView类型的属性,并使用代码或设计器对其进行分配,并将其用于UserControl

public DataGridView ClientsGrid { get; set; } 

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