disable copy and paste in datagridview

久未见 提交于 2019-12-04 17:32:34

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.

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:

Example:

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;
  }
}

You can try this.

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            TextBox tb = e.Control as TextBox;
            tb.ShortcutsEnabled = false;
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!