DataGridView editing cells on WinForms

10,369

There is no good out of the box way of doing this. The closest there is is to set the EditMode of the grid to EditOnEnter but that means you only need two clicks, not three.

You will need to write your own column type.

Someone has done just that here.

I haven't checked if that example handles up and down - if it doesn't then you were on the right track with the SelectionStart and SelectionLength properties, just grab the caret position of the cell you are leaving and apply it to the new cell.


It turns out that setting these properties is a little bit more involved that I remembered (possibly because I was already using a MaskedTextBox custom column type last time I did this).

The code below (in c# but the principle holds for vb.Net and I can give the vb code if you can't convert it yourself) works happily - could be tidied up by putting it into a custom control but I'll leave that as an exercise :)

First I add a handler for the EditingControlShowing event:

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    DataGridViewTextBoxEditingControl t = e.Control as DataGridViewTextBoxEditingControl;
    current_control = t;
    t.Leave += new EventHandler(t_Leave);
}

In the method above current_control is a form level private variable. The event handler for t looks like this:

void t_Leave(object sender, EventArgs e)
{
    cell_caret_pos = current_control.SelectionStart;
}

There again we have a class level private field - cell_caret_pos.

Then what I found was that to set SelectionStart and SelectionLength you need to work within the CellEnter event handler:

private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
    dataGridView1.BeginEdit(false);
    DataGridViewTextBoxEditingControl editControl =
        (DataGridViewTextBoxEditingControl)dataGridView1.EditingControl;

    if (cell_caret_pos != 0)
    {
        editControl.SelectionStart = cell_caret_pos;
        editControl.SelectionLength = 0;
    }
} 
Share:
10,369
Nick Giles
Author by

Nick Giles

Updated on June 04, 2022

Comments

  • Nick Giles
    Nick Giles almost 2 years

    I have a Windows Form VS2010 .NET 4 project with a standard DataGridView bound to a datasource on a form.

    The grid has a text column that I want to be a point and edit at the character clicked to. Like normal textbox/editors when you click on the character you want to adjust. If possible I would also like to use the UP/DOWN keys to move between rows but would like the cursor to move to the same character position obviously in the same column without selecting the entire text.

    I have tried a few things:

    DataGridView1.ClearSelection()

    DataGridView1.BeginEdit(False)

    The BeginEdit just puts the cursor at the end of the text, which means another click to point to the character position for editing.

    I know a Commercial grid like DevExpress defaults to editing in which you can click to the correct character position with one click but obviously costs money.

    I have tried in the DataGridView1_EditingControlShowing event

    If TypeOf e.Control Is System.Windows.Forms.DataGridViewTextBoxEditingControl Then
            Dim tb As TextBox = e.Control
            tb.SelectionStart = 5
            tb.SelectionLength = 5
    End If
    

    But this does nothing.

    I am just trying to remove the two or three clicks to get to the character position that needs adjustment.

    I haven't looked at a Custom DataColumn as yet.

    Any suggestions would be greatly appreciated.