Commit and creating new row with two enter presses

5 posts, 1 answers
  1. Marcel
    Marcel avatar
    42 posts
    Member since:
    Jan 2014

    Posted 15 Nov 2016 Link to this post

    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

  2. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    665 posts

    Posted 17 Nov 2016 Link to this post

    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.
  3. Marcel
    Marcel avatar
    42 posts
    Member since:
    Jan 2014

    Posted 17 Nov 2016 in reply to Stefan Nenchev Link to this post

    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

  4. Answer
    Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    665 posts

    Posted 21 Nov 2016 Link to this post

    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.
  5. Marcel
    Marcel avatar
    42 posts
    Member since:
    Jan 2014

    Posted 23 Nov 2016 in reply to Stefan Nenchev Link to this post

    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

Back to Top