简体   繁体   中英

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?

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?


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.

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

readOnly = False

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

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 Tag property on the column regarding it is read only or not.

  • Set your grid edit settings to EditOnEnter

  • Then, use the EditingControlShowing event 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.


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.

You can set selection mode to 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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