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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20860881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:18:25  来源:igfitidea点击:

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

.netvb.netwinformsdatagridview

提问by eternal

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. 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,但这会禁用单元格上文本的突出显示。我希望他们能够从单元格中复制。是否有像 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. 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.

文本框具有我正在寻找的效果:我有一个带有 readonly = true 初始文本的文本框字段。我可以使用鼠标突出显示该文本框中的部分文本(然后复制它)。文本框的内容不可编辑。这就是我想要的效果,但我想在 fullrowselectmode 中使用 datagridview 来做到这一点。

Currently I have: selectionMode = fullRowSelect (I want to be able to select an entire row, not by cell)

目前我有: selectionMode = fullRowSelect (我希望能够选择整行,而不是按单元格)

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!

谢谢!

回答by Katy

You should set the readonly property of your DataGridView to true, then it will not be editable while users can copy the cells.

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

回答by OSKM

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 selesctionmodeto cellselectotherwise you will be copying an entire row

这允许用户选择和复制单元格但不能编辑它,但您的要求非常令人困惑 - 如果您想复制单个单元格,则需要将 设置selesctionmodecellselect否则您将复制整行

回答by Larry

Here is something I am using:

这是我正在使用的东西:

  • First make all your column ReadOnly=false, because you will have to override its default behavior.

  • Put true or false in the Tagproperty on the column regarding it is read only or not.

  • Set your grid edit settings to EditOnEnter

  • Then, use the EditingControlShowingevent to change the textbox properties that pops every time the user clicks in the cell. Regardless the textbox is read only or not, the user will be able to select and copy the content.

  • 首先让你的所有列ReadOnly=false,因为你必须覆盖它的默认行为。

  • 将 true 或 falseTag放在列上的属性中,关于它是否只读。

  • 将您的网格编辑设置设置为 EditOnEnter

  • 然后,使用该EditingControlShowing事件更改每次用户单击单元格时弹出的文本框属性。无论文本框是否只读,用户都可以选择和复制内容。



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 Tagthing is not the cleanest, but there are plenty other way to store some custom columns attributes.

Tag事情是不是干净的,但也有很多其他的方式来存储一些自定义列属性。

回答by Gie

You can set selection mode to RowHeaderSelect. It allows you to copy by cell or by row.

您可以将选择模式设置为RowHeaderSelect。它允许您按单元格或按行复制。

回答by mohammed jeaber

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