This is a migrated thread and some comments may be shown as answers.

Commit and creating new row with two enter presses

4 Answers 73 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Marcel
Top achievements
Rank 1
Marcel asked on 15 Nov 2016, 08:43 AM

Hello,

We want to use the enter key to traverse between the cells in the row of a RadGridView, this works fine at the moment only the transition to the next row is a problem.

We want the RadGridView to behave as follows,

  • When the last cell is reached, a enter will commit the changes in the last cell without creating a new row;
  • If the user gives another enter, a new row is created and focus is traversed to the first editable cell in the new row.

We have created a CustomKeyboardCommandProvider as suggested in this blog post.

The function ProvideCommandsForKey(..) is only called for the first enter, then the keyboardfocus is moved out of the grid while the last cell has still some sort of focus, a rectangle is shown in the cell, but it doesn't respond on the keyboard enter presses anymore. We tried using commands to select the cell or row after the commit but these are not working, the focus is still leaving the grid after the commit is done.

 

public override IEnumerable<ICommand> ProvideCommandsForKey(Key key)
{
    List<ICommand> commandsToExecute = base.ProvideCommandsForKey(key).ToList();
 
    /* IF Enter key is pressed and we are in the last cell */
    if (key == Key.Enter && (_parentGrid.CurrentColumn.DisplayIndex == _parentGrid.Columns.Count - 1))
    {
        if (_firstEnterHandled == false)
        {
            _firstEnterHandled = true;
            commandsToExecute.Clear();
            commandsToExecute.Add(RadGridViewCommands.CommitCellEdit);
            commandsToExecute.Add(RadGridViewCommands.SelectCurrentUnit);  // DOESNT WORK
        }
        else
        {
            commandsToExecute.Clear();
            commandsToExecute.Add(RadGridViewCommands.BeginInsert);
                 
            /* Reset flag */
            _firstEnterHandled = false;
        }
    }
    else
    {               
        /* Reset flag */
        _firstEnterHandled = false;
    }
 
    return commandsToExecute;
}

 

 

Any help would be appreciated.

Marcel

4 Answers, 1 is accepted

Sort by
0
Stefan Nenchev
Telerik team
answered on 17 Nov 2016, 03:51 PM
Hi Marcel,

Instead of adding the BeginInsert command in the CommandsToExecute list, you can directly call the BeginInsert method on the parent grid:

if (key == Key.Enter && (_parentGrid.CurrentColumn.DisplayIndex == _parentGrid.Columns.Count - 1))
            {
                if (_firstEnterHandled == false)
                {
                    _firstEnterHandled = true;
                    commandsToExecute.Clear();
                    commandsToExecute.Add(RadGridViewCommands.CommitCellEdit);
                    commandsToExecute.Add(RadGridViewCommands.SelectCurrentUnit);  // DOESNT WORK
                }
                else
                {
                    commandsToExecute.Clear();
                    _parentGrid.BeginInsert();
                    /* Reset flag */
                    _firstEnterHandled = false;
                }
            }

Please try it and update the thread whether it works for you.



Regards,
Stefan Nenchev
Telerik by Progress
Telerik UI for WPF is ready for Visual Studio 2017 RC! Learn more.
0
Marcel
Top achievements
Rank 1
answered on 17 Nov 2016, 04:33 PM

Hi Stefan,

 

Unfortunately this will not work because the else-block is never reached. The problem is that after the CommitCellEdit command is executed the grid loses focus, so the second enter will only be seen by the grid if we move the focus back to the grid manually.

Any ideas why the grid loses focus after the CommitCellEdit command is executed?

Regards,

Marcel

0
Accepted
Stefan Nenchev
Telerik team
answered on 21 Nov 2016, 03:03 PM
Hi Marcel,

Please check the attached sample project. When the RadGridView is in edit mode, pressing the enter key would result in moving to the next cell. If the last cell is reached - the data is committed on pressing enter the first time. On another occasion, a new row is added. Is this the behavior you are looking for?

Regards,
Stefan Nenchev
Telerik by Progress
Telerik UI for WPF is ready for Visual Studio 2017 RC! Learn more.
0
Marcel
Top achievements
Rank 1
answered on 23 Nov 2016, 12:15 PM

Hi Stefan,

 

Thanks for the sample, got it working.

Also needed to set FocusManager.IsFocusScope to false in the RadGridView.

Now it works fine, thanks for the support.

 

Regards,

 

Marcel

Tags
GridView
Asked by
Marcel
Top achievements
Rank 1
Answers by
Stefan Nenchev
Telerik team
Marcel
Top achievements
Rank 1
Share this question
or