如何将DataGridViewCell转换为控件 [英] How to convert a DataGridViewCell to a Control
问题描述
我正在尝试将DataGridView单元格转换为控件:
I am trying to convert a DataGridView cell to a control:
Control dat = new Control();
dat = Convert.ChangeType(dataGridView1[colIndex,rowIndex], typeof(Control));
我正在从索引代码中获取colIndex和rowIndes的值。问题是即使我尝试了许多代码转换,也无法正常工作。
I am fetching the values of colIndex and rowIndes from a index code. The problem is even though I tried many codes to convert, it does not work.
我得到的异常是:
不能隐式地将对象转换为控件。存在显式转换(是否缺少转换?)
Can not implicitly convert an object to a control. An explicit convertion exist(are u missing a cast?)
推荐答案
由 DataGridViewCell
托管,您使用当单元格处于编辑模式时,单元格的 EditingControl
此属性返回 System.Windows.Forms.Control
。
您也可以在 DataGridViewEditingControlShowing
事件中获得控件-的
的类型为 Control
属性DataGridViewEditingControlShowingEventArgs System.Windows.Forms.Control
。
You can also get at the control within the DataGridViewEditingControlShowing
event - the Control
property of DataGridViewEditingControlShowingEventArgs
is of type System.Windows.Forms.Control
.
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
Control c = e.Control;
}
如果您想在其他时间访问控件,那么(我相信)您很不走运-我说这主要是基于 DataGridViewEditingControlShowing
:
If you want to access the control at other times then (I believe) you are out of luck - I say that based mainly on this quote from the MSDN docs on DataGridViewEditingControlShowing
:
DataGridView控件一次托管一个编辑控件,并且只要单元格类型在两次编辑之间不更改
时,
就会重新使用该编辑控件。
The DataGridView control hosts one editing control at a time, and reuses the editing control whenever the cell type does not change between edits.
这篇关于如何将DataGridViewCell转换为控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!