简体   繁体   English

禁用对datagridview的编辑,但仍允许突出显示以复制和粘贴单元格

[英]Disable edits on datagridview but still allow for highlighting to copy and paste cells

Is there a property (or workaround) that will but not allow editing in a datagridview BUT also allow for the highlighting of text in cells? 是否有一个属性(或变通办法)可以但不允许在datagridview中进行编辑,但允许在单元格中突出显示文本?

Currently users are able to highlight/copy and edit text in cells (but no changes are being made). 当前,用户能够突出显示/复制和编辑单元格中的文本(但未进行任何更改)。 They attempt to edit the text in cells and then become confused when their changes are not being saved. 他们尝试编辑单元格中的文本,然后在不保存更改时感到困惑。 I want it so the cells do not appear editable. 我想要它,因此单元格不会显示为可编辑的。

I tried setting the readonly property = true, but that disables highlighting of text on the cell. 我尝试将readonly属性设置为true,但是禁用了单元格上文本的突出显示。 I want them to be able to copy from the cells. 我希望他们能够从单元中复制。 Is there a property like readonly = true that still allows for highlighting of cells? 是否有像readonly = true这样的属性仍可以突出显示单元格?


EDIT- For Clarification: 编辑-为了澄清:

The textbox has the effect I am looking for: I have a textbox field with initial text with readonly = true. 文本框具有我要查找的效果:我有一个文本框字段,其初始文本为readonly = true。 I can use my mouse to highlight parts of the text in that textbox (and then copy it). 我可以使用鼠标突出显示该文本框中的部分文本(然后将其复制)。 The contents of the textbox is not editable. 文本框的内容不可编辑。 This is the effect I want, but I want to do this with the datagridview in fullrowselectmode. 这是我想要的效果,但是我想使用fullrowselectmode中的datagridview做到这一点。

Currently I have: selectionMode = fullRowSelect (I want to be able to select an entire row, not by cell) 当前我有:selectionMode = fullRowSelect(我希望能够选择整行,而不是按单元格)

readOnly = False readOnly = False

EditMode = EditOnKeystrokeOrF2 EditMode = EditOnKeystrokeOrF2

These settings allow users to "double click" on a cell and then highlight text within any cell. 这些设置允许用户“双击”单元格,然后突出显示任何单元格中的文本。 This is the effect I want, BUT the only problem with these settings is users can also type more/delete text in that cell. 这是我想要的效果,但是这些设置的唯一问题是用户还可以在该单元格中键入更多/删除文本。

Thanks! 谢谢!

您应该将DataGridView的readonly属性设置为true,这样在用户可以复制单元格时将无法编辑该属性。

You can use: 您可以使用:

DataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically

this alows the user to select and copy the cell but not edit it but your requirements are very slightly confusing - if you are wanting to copy a single cell you will need to set the selesctionmode to cellselect otherwise you will be copying an entire row 这alows用户选择和复制单元格,但不能编辑它,但你的要求是非常略显混乱-如果你是想复制一个单元格,你将需要设置selesctionmodecellselect否则你会被复制一整行

Here is something I am using: 这是我正在使用的东西:

  • First make all your column ReadOnly =false, because you will have to override its default behavior. 首先,将所有列都设为ReadOnly = false,因为您将不得不覆盖其默认行为。

  • Put true or false in the Tag property on the column regarding it is read only or not. 关于只读的列的Tag属性中,将true或false设置为false。

  • Set your grid edit settings to EditOnEnter 将网格编辑设置设置为EditOnEnter

  • Then, use the EditingControlShowing event to change the textbox properties that pops every time the user clicks in the cell. 然后,使用EditingControlShowing事件更改每次用户单击单元格时弹出的文本框属性。 Regardless the textbox is read only or not, the user will be able to select and copy the content. 无论文本框是只读还是不可读,用户都可以选择和复制内容。


private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) {
    if(!(e.Control is TextBox))
        return;

    var txt = e.Control as TextBox;

    if(true.Equal(grid.CurrentCell.OwningColumn.Tag)) {
        txt.ReadOnly = true;
    }
    else {
        txt.ReadOnly = false;
    }
}

The Tag thing is not the cleanest, but there are plenty other way to store some custom columns attributes. Tag并不是最干净的东西,但是还有许多其他方式可以存储一些自定义列属性。

You can set selection mode to RowHeaderSelect . 您可以将选择模式设置为RowHeaderSelect It allows you to copy by cell or by row. 它允许您按单元格或按行复制。

Solved like this 这样解决

Private Sub dgv_CellValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles dgv.CellValidating
    If dgv.IsCurrentCellDirty Then
        e.Cancel = True
        SendKeys.Send("{ESC}")
    End If
End Sub

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

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