简体   繁体   English

DataGridViewCell编辑模式下的Ctrl + c复制整行

[英]Ctrl + c in DataGridViewCell edit mode copies the whole row

I have a datagrid that has one column with text that I would like to allow users to copy text out of. 我有一个数据网格,其中有一列文本,我希望允许用户复制文本。 I have set up routines to be able to copy the entire cell, or row, but I am having issues when editing the cell and typing CTRL + C. 我已经设置了例程来复制整个单元格或行,但是在编辑单元格和键入CTRL + C时遇到问题。

This is the code I am using to allow the cell to be edited. 这是我用来编辑单元格的代码。 Once inside, I can highlight text and right click it for copy. 进入后,我可以突出显示文本并右键单击它进行复制。 This works just fine, it is if I highlight text and type CTRL + C that it then copies the row, instead of the highlighted text. 这很好用,如果我突出显示文本并键入CTRL + C然后复制行,而不是突出显示的文本。

I don't want to have to create my own class, and if it isn't possible I will just leave it as it is. 我不想创建自己的类,如果不可能,我会保持原样。

private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView1.EditingControl == null ||
    dataGridView1.CurrentCell.EditType != typeof (DataGridViewTextBoxEditingControl))
    return;
    dataGridView1.CancelEdit();
    dataGridView1.EndEdit();
}

private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView1.CurrentCell.EditType == typeof(DataGridViewTextBoxEditingControl))
    {
    dataGridView1.BeginEdit(false);
    }
}

I was having the exact same problem as the OP and needed a solution with one slight difference. 我遇到与OP完全相同的问题,需要一个稍有不同的解决方案。 I needed DataGridViewSelectionMode.FullRowSelect not CellSelect. 我需要DataGridViewSelectionMode.FullRowSelect而不是CellSelect。 I was able to achieve this by doing the following: 通过执行以下操作,我能够实现此目的:

datagridview1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
datagridview1.ClipboardCopyMode = DataGridViewClipboardCopyMode.Disable;
datagridview1.MultiSelect = false;

I believe setting ClipboardCopyMode disable defeats the built-in grid function and what's left is standard MS Windows behavior. 我相信设置ClipboardCopyMode禁用会破坏内置网格功能,剩下的就是标准的MS Windows行为。 So I am able to select text in an editable cell and enter ctrl-C to copy "just" the selected text. 因此,我可以在可编辑单元格中选择文本并输入ctrl-C来“复制”所选文本。
-OR- It's quite possible there has been a behavior change between .NET versions since this was first posted. - 或者 - 很可能.NET版本之间的行为发生了变化,因为这是第一次发布的。 In any case this works for me using Visual Studio 2013, .NET 4.5 and windows 7. 无论如何,这适用于我使用Visual Studio 2013,.NET 4.5和Windows 7。

Also important to note: I my case I only cared about being able to copy/paste editable columns/cells. 同样重要的是要注意:我的情况我只关心能够复制/粘贴可编辑的列/单元格。 This solution achieves this. 该解决方案实现了这一点。 Right click still works too. 右键单击仍然有效。

Other grid settings that might be important to viewers: 其他可能对观看者很重要的网格设置:

datagridview1.EditMode = DataGridViewEditMode.EditProgrammatically;
datagridview1.EnableHeadersVisualStyles = false;
datagridview1.RowHeadersVisible = false;

If you are having the SelectionMode property as FullRowSelect then it will copy the entire row even if a cell is in edit mode. 如果您将SelectionMode属性设置为FullRowSelect那么即使单元格处于编辑模式,它也将复制整行。 Change the value to CellSelect . 将值更改为CellSelect Set the below properties to copy only the editing cell content using CTRL + C. 设置以下属性以使用CTRL + C仅复制编辑单元格内容。

dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
dataGridView1.MultiSelect = false;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM