如何向剪贴板发送数据网格视图内容,例如CTRL-C [英] How to send to clipboard datagridview content like CTRL-C
问题描述
如何将DataGridView中的选定内容发送到剪贴板,以模拟CTRL-C行为。
How to send to clipboard selected content from DataGridView, simulating CTRL-C behavior.
这无法按预期进行:
Clipboard.SetText(this.dataGridView1.SelectedCells.ToString());
用户需要在Excel中粘贴。 CTRL-C正常工作,但是我需要为上下文菜单编写脚本。
User needs to paste in Excel. CTRL-C is working fine, but I need to script for context menu.
推荐答案
SelectedCells
是 CellCollection
,因此没有有用的ToString方法。
SelectedCells
is a CellCollection
and as such has no useful ToString method.
如果要复制只需一个单元格,您就必须确定哪个单元格,然后复制其值
,例如:
If you want to copy just one cell you have to decide which and then copy its Value
, e.g.:
Clipboard.SetText(this.dataGridView1.SelectedCells[0].Value.ToString());
如果您要复制单元格的范围,事情将会变得更加复杂,尤其是如果单元格范围不连续。.但是Crtl-C也无法在DGV->剪贴板-> Excel的单元格范围内工作。
If you want to copy a range of cells things get more complicated, especially if the cell range is not contiguous.. But Crtl-C will also not work over a cell range going DGV -> clipboard -> Excel.
对于简单范围,如果需要,您可以通过将单元格值与 TABs
关联来构造必要的字符串向右移动一个单元格,然后单击 CRLF
转到下一行。您需要吗?
For a simple range, if you need it, you should be able to construct the necessary string by concatenating the cells values with TABs
to move a cell to the right and CRLF
to go to the next row.. Will you need that?
注意:在尝试访问 SelectedCells [0]
之前,您需要检查 SelectedCells.Count> 0
!
Note: Before trying to access SelectedCells[0]
you need to check if SelectedCells.Count > 0
!
这篇关于如何向剪贴板发送数据网格视图内容,例如CTRL-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!