eternal
eternal

Reputation: 99

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!

Upvotes: 2

Views: 21747

Answers (5)

mohammed jeaber
mohammed jeaber

Reputation: 1

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

Upvotes: 0

Gie
Gie

Reputation: 1

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

Upvotes: 0

Larry
Larry

Reputation: 18031

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.

Upvotes: 1

OSKM
OSKM

Reputation: 728

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

Upvotes: 1

Katy
Katy

Reputation: 296

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

Upvotes: 4

Related Questions