DataGridView - how to NOT select-all-text when entering a cell?

chris_asa

Active member
Joined
Sep 4, 2009
Messages
26
Location
Switzerland
Programming Experience
10+
Hi,

I have an unbound VS2008 DataGridView in which the users will type/edit/re-edit 10...30 rows until they get the desired result.

When you tab or arrow or mouse into a cell that already contains data, the entire cell contents are selected - how can I arrange for an edit cursor instead?

If mousing, a second click does it, else a left-or-right arrow does it, but I want to force this as default behaviour. If the user !!bizarrely!! wants to overwrite everything in the cell they can ctl-A (or mouse).

Thanks for any hints. Chris
 
Hi,

Try to change DataGridView property 'EditMode' to EditOnEnter. Is that what you were looking for?

If you don't want to get the text in cell selected (only edit cursor on the end) you can send right arrow key after every cell change. You can do this like that:

VB.NET:
    Private Sub DataGridView1_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
        SendKeys.Send("{RIGHT}")
    End Sub

If you need to add new cells (not only edit existing ones), you should check if the entered cell has any text in it. In other case you will jump to the next right cell after selecting an empty cell. You might do it like that:

VB.NET:
    Private Sub DataGridView1_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
        If DataGridView1.CurrentCell.Value <> "" Then
            SendKeys.Send("{RIGHT}")
        End If
    End Sub

Hope that helps :)
 
Last edited:
Hi homer,

Thanks for that!

VB.NET:
    Private Sub dgvLines_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvLines.CellEnter
        If dgvLines.CurrentCell.Value <> "" Then
            SendKeys.Send("{Right}")
        End If
    End Sub
...does a pretty good job. It works when you enter a cell with TAB / arrow-keys / mouse, but fails (entire field selected) when you enter with SHIFT-TAB.

Chris
 
If you start cell edit with the default F2 key the cursor is placed at end of text with no selection.
 
Hi John,

Sure!

But how many 'real-world' Win-apps are actually used by users-who-hit-F2.

I would prefer my UI to be:
a- not shocking to reasonably adept Win-users (and/but with the greatest respect to my [paying!!!!] users, I try to code for them to be as dumb as bricks)
b- fast, convenient, intuitive, and above all like-everything-else-they-work-with.

In the end, I can live with the SHIFT-TAB = BROKEN issue, and I can probably explain F2-or-live-with-it as the option.

Is it perfect.............

regards Chris
 
If you want to you can customize these things. For example, in this case you have a column (DataGridViewTextBoxColumn) that has multiple cells (DataGridViewTextBoxCell), which uses an editing control (DataGridViewTextBoxEditingControl). The editing control implements a common interface IDataGridViewEditingControl that happens to have a PrepareEditingControlForEdit(selectAll) method, the selectAll parameter is a Boolean argument that toggles if the editing control should select all content when cell is going into edit mode. All these classes can be inherited and customized, and the PrepareEditingControlForEdit method is also overridable.

Here is a sample class set, I used "NS" name prefix that could mean "NoSelect...", the NSDataGridViewTextBoxEditingControl here always sets False for selectAll thus never selects the text when starting edit :
VB.NET:
Public Class NSDataGridViewTextBoxColumn
    Inherits DataGridViewTextBoxColumn
    Public Sub New()
        Me.CellTemplate = New NSDataGridViewTextBoxCell
    End Sub
End Class

Public Class NSDataGridViewTextBoxCell
    Inherits DataGridViewTextBoxCell
    Public Overrides ReadOnly Property EditType() As System.Type
        Get
            Return GetType(NSDataGridViewTextBoxEditingControl)
        End Get
    End Property
End Class

Public Class NSDataGridViewTextBoxEditingControl
    Inherits DataGridViewTextBoxEditingControl
    Public Overrides Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean)
        MyBase.PrepareEditingControlForEdit(False)
    End Sub
End Class
When you add these classes you can swap the existing textbox columns for NSDataGridViewTextBoxColumn ones, this can be done in designer (DGV column editor) or in code.

Since this would break default HCI, it would probably be a good idea to provide a property for this behaviour and an option in app for user to select it.
 
Back
Top