Programatically enter edit mode

May 8, 2009 at 11:41 AM

Hi All,

I am on the latest version of the datagrid and trying to enter edit mode programatically when the grid receive focus from tabbing from another control.

I am trying to achieve using the form without using the mouse. The user has to tab through 3 text boxes then onto the grid. For the text boxes I handled the PreviewKeyDown event, checked for the Enter key and give focus to the next text box (if it was the Enter key) and swalling the enter key. This is all 100% until I give focus to the grid from the 3rd text box. The grid gets focus, but I still need to select the first row/cell with the mouse to begin editing.

So question is... How to programatically enter edit mode on the first editable cell in a grid when it receives focus.


May 8, 2009 at 11:47 AM

Call DataGrid.BeginEdit(). That should do the trick.

May 8, 2009 at 12:14 PM

Ye, first thing I tried. It did nada.

Here is the code from the 3rd textbox enter key press.



<font size="2" color="#0000ff"><font size="2" color="#0000ff">


</font></font><font size="2" color="#0000ff">



void BankedTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
  if (e.Key == Key.Enter)
    grid.Focus(); // Gives focus
    grid.BeginEdit(); // Does nada
    e.Handled =
true; // Swallows the enter key

May 9, 2009 at 8:50 PM
Edited May 9, 2009 at 8:52 PM

Hello Dominion,

You must set the selected row and the selected cell before attempting BeginEdit. These are the two lines needed to accomplish what you want:

grid.SelectedIndex = 0; //Select the first row.
grid.CurrentCell = grid.SelectedCells[0]; //Set the CurrentCell to the first cell of the SelectedRow.

You can change the logic to select the cell you wanted, I just selected the first.

void BankedTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
  if (e.Key == Key.Enter)
    grid.Focus(); // Gives focus
    grid.SelectedIndex = 0; //Select the first row.
    grid.CurrentCell = grid.SelectedCells[0]; //Set the CurrentCell to the first cell of the SelectedRow.
    grid.BeginEdit(); // you can check that now this method returns true so the edition took place.
    e.Handled = true; // Swallows the enter key


May 10, 2009 at 8:46 PM

Thank-you Ajadex. That did the trick. :)

May 10, 2009 at 10:34 PM

Glad to help, ;-)

May 25, 2009 at 1:19 PM

I am trying to set a complte row into the edit mode when a user clicks into a button inside the row, that should get edited.

In the Click-Eventhandler I am trying to set the whole row into edit mode, but the only thing I achieved with this hints is to get one cell into edit mode. Can somebody help me?

May 25, 2009 at 3:35 PM

I'm not sure I understand your question. For one, what exactly should it mean that a full row, as opposed to a cell, is in edit mode?

If you explain what underlying problem you are trying to solve by "setting the whole row into edit mode", maybe someone can propose an alternative solution.

May 26, 2009 at 7:21 AM

What I mean with setting the full row into edit mode is that when the user clicks the button not only in one cell the editing template is shown but in all cells of the selected row the editing template should be shown, so the user can edit the whole row without double clicking on each cell seperately.

May 26, 2009 at 1:36 PM

When a cell gets into edit mode, the whole row gets into edit mode.  As you tab to the next cell in the row, that cell gets into edit mode.  That doesn't suffice your requirement?  If you really just want all cells to go into edit mode at the same time (and lose that IEditableObject behavior) then you may be better of with a custom ListView that implements IEditableCollectionView.  Here is an example of that,

May 26, 2009 at 1:44 PM

Thankyou vinsibal.
I will have a look at the custom ListView...

May 27, 2009 at 8:07 AM

The ListBox does not work for me. I have a Style Guide that tells me that my grid has to have Columns with headers and so on.
At first I thought I could take a ListView, because it has Columns and headers when I am using it with the GridView inside, but then I have the same problem, that there are the CellTemplates...