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.
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.
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);
}
}
The DataGrid control supports cell and row editing functionality. By default, you can edit items directly in the DataGrid. The user can enter edit mode in a cell by pressing F2 key or double tapping on a cell. Alternatively, you can set the DataGridColumn.
To copy the selection to the clipboard or replace the selection with the clipboard contents, you can use the Ctrl+C or Ctrl+V shortcut respectively, or select the corresponding command – Copy or Paste – from the cell's context menu.
I was having the exact same problem as the OP and needed a solution with one slight difference. I needed DataGridViewSelectionMode.FullRowSelect not 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. So I am able to select text in an editable cell and enter ctrl-C to copy "just" the selected text.
-OR- It's quite possible there has been a behavior change between .NET versions since this was first posted. In any case this works for me using Visual Studio 2013, .NET 4.5 and 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. Change the value to CellSelect
. Set the below properties to copy only the editing cell content using CTRL + C.
dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
dataGridView1.MultiSelect = false;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With