Reputation: 99
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
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
Reputation: 1
You can set selection mode to RowHeaderSelect
. It allows you to copy by cell or by row.
Upvotes: 0
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
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
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