禁用复制并粘贴在datagridview中 [英] disable copy and paste in datagridview
问题描述
我是.net的初学者.这可能是一个愚蠢的问题.我想禁用 ctrl + c 和 ctrl + v 键盘快捷键.
I am a beginner in .net. This could be a silly question. I want to disable ctrl + c and ctrl + v keyboard shortcuts.
在这里问之前,我尝试了以下代码 link1 和链接2 (不起作用)
Before asking here I tried these codes link1 and link2 (not working)
private void dgvMain_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
this.dgvMain.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;
}
private void dgvMain_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
this.dgvMain.ClipboardCopyMode = DataGridViewClipboardCopyMode.Disable;
}
还有
this.dgvMain.ClipboardCopyMode = DataGridViewClipboardCopyMode.Disable;
dgvMain
是datagridview
我可能在这里错过了一些东西.
dgvMain
is datagridview
I maybe missing something here.
我已更改的datagridview的属性是:
The properties for my datagridview which I have altered are:
AllowUserToResizeColumns -- False
AllowUserToResizeRows -- False
ClipboardCopyMode -- disable
ColumnsHeadersHeightSizeMode -- AutoSize
Dock -- Fill
ReadOnly -- True
TabStop -- False
请帮助
预先感谢.
Please help
Thanks in Advance.
推荐答案
您不会拼写出不起作用的部分,因此我只能猜测您是指网格的TextBox部分.
You don't spell out the not-working part, so I can only guess that you are referring to the TextBox part of the grid.
仅具有ClipboardCopyMode = Disable
应该足够了,但是如果单元格的TextBox处于编辑模式,则该属性将被忽略.您必须自己禁用按键和ContextMenu:
It should be enough to just have ClipboardCopyMode = Disable
but if the TextBox of the cell is in edit mode, that property gets ignored. You would have to disable the keys and the ContextMenu yourself:
示例:
public Form1()
{
InitializeComponent();
dgvMain.ClipboardCopyMode = DataGridViewClipboardCopyMode.Disable;
dgvMain.EditingControlShowing += dgvMain_EditingControlShowing;
}
void dgvMain_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
TextBox tb = e.Control as TextBox;
if (tb != null) {
tb.ContextMenuStrip = new ContextMenuStrip();
tb.KeyDown -= TextBox_KeyDown;
tb.KeyDown += TextBox_KeyDown;
}
}
void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && (e.KeyCode == Keys.C | e.KeyCode == Keys.V)) {
e.SuppressKeyPress = true;
}
}
这篇关于禁用复制并粘贴在datagridview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!