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

Custom Keyboard Provider

11 Answers 195 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Koren
Top achievements
Rank 1
Koren asked on 16 May 2011, 09:13 PM
Our users need to be able to enter data in the grids using the number pad which means that I need the Enter key to work like the Tab key for adding data.  I got this close to working using a CustomKeyboardCommandProvider where I assign the Enter key to the Tab key commands.  However, I would actually prefer to only have it work this way if I am inserting a record and let it work as normal if the user is just trying to go back and update a field.  I use the AddingNewDataItem to add a new object when inserting a record.  Is there any way in the CustomKeyboardCommandProvider to know when I am inserting vs updating a record? Or is there a better way to get this to work?

Also, if they are in insert mode, I would like to be able to take them to a new record after the current record is committed. Can I add a BeginInsert command to the list of commands after the Commit to get this to work?  I noticed that the tab key list does not include a CommitEdit command when it wraps from one record to another so I am not sure where to add this BeginInsert?

thanks!! 

11 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 18 May 2011, 02:18 PM
Hi Koren,

You may handle the AddingNewDataItem and RowEditEnded events like follows:

void playersGrid_AddingNewDataItem(object sender, Telerik.Windows.Controls.GridView.GridViewAddingNewEventArgs e)
        {
            e.NewObject = new Player();
            this.playersGrid.KeyboardCommandProvider = new CustomKeyboardCommandProvider(this.playersGrid);
        }
 
        void playersGrid_RowEditEnded(object sender, GridViewRowEditEndedEventArgs e)
        {
            this.playersGrid.KeyboardCommandProvider = new DefaultKeyboardCommandProvider(this.playersGrid);
        }


The CustomKeyboardCommandProvider may be defined as below:
public class CustomKeyboardCommandProvider : DefaultKeyboardCommandProvider
    {
        private GridViewDataControl parentGrid;
 
        public CustomKeyboardCommandProvider(GridViewDataControl grid)
            : base(grid)
        {
            this.parentGrid = grid;
        }
 
        public override IEnumerable<ICommand> ProvideCommandsForKey(Key key)
        {
            List<ICommand> commandsToExecute = base.ProvideCommandsForKey(key).ToList();
            if (key == Key.Enter)
            {
                commandsToExecute.Clear();
                commandsToExecute.Add(RadGridViewCommands.MoveNext);
                commandsToExecute.Add(RadGridViewCommands.BeginEdit);
            }
 
            return commandsToExecute;
        }
    }
 
Let me know whether this corresponds to your requirements.

Best wishes,
Maya
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Koren
Top achievements
Rank 1
answered on 18 May 2011, 07:30 PM
Brilliant.  Worked just as I needed.

thanks!
0
Koren
Top achievements
Rank 1
answered on 27 Jan 2015, 03:18 PM
I just upgraded to Q3 2014 and this keyboard handler stopped working.  When I get to the end of the row where it should commit and begin another row, it just commits and goes out of edit mode.  Was there a change in 2014 that I should be aware of?
0
Koren
Top achievements
Rank 1
answered on 27 Jan 2015, 03:21 PM
Sorry - I just realized that my logic is a little different than what was posted...

public override IEnumerable<ICommand> ProvideCommandsForKey(Key key)
        {
            List<ICommand> commandsToExecute = base.ProvideCommandsForKey(key).ToList();
 
            bool _shiftIsPressed = (Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift;
            int _lastColumnIndex = 0;
 
            if (key == Key.Tab || key == Key.Enter)
            {
                bool _onLastColumn = false;
 
                if (!String.IsNullOrEmpty(_lastColumnName))
                {
                    if (this.parentGrid.CurrentColumn.UniqueName.ToLower() == _lastColumnName.ToLower())
                        _onLastColumn = true;
                }
                else
                {
                    _lastColumnIndex = FindLastEditableColumn();
 
                    var lastColumn = this.parentGrid.Columns[_lastColumnIndex] as Telerik.Windows.Controls.GridViewColumn;
                    if (this.parentGrid.CurrentColumn == lastColumn)
                        _onLastColumn = true;
                }
 
                if (_onLastColumn)
                {
                    if (_shiftIsPressed)
                    {
                        commandsToExecute.Clear();
                        commandsToExecute = base.ProvideCommandsForKey(Key.Tab).ToList();
                    }
                    else
                    {
                        commandsToExecute.Clear();                       
                        commandsToExecute.Add(RadGridViewCommands.CommitEdit);
                        commandsToExecute.Add(RadGridViewCommands.BeginInsert);
                    }
                }
                else
                    if (key == Key.Enter)
                    {
                        commandsToExecute.Clear();
                        commandsToExecute = base.ProvideCommandsForKey(Key.Tab).ToList();
                    }
            }           
 
            _lastKey = key;
            return commandsToExecute;
             
        }
0
Maya
Telerik team
answered on 29 Jan 2015, 12:58 PM
Hi Koren,

I might be missing something here, but did you manage to resolve the issue that you had or do you need an assistance ? 

Regards,
Maya
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Koren
Top achievements
Rank 1
answered on 19 Feb 2015, 11:08 PM
No I did not fix this issue.  It commits the edit but does not BeginInsert at the end of the row like it did before for either the Tab or the Enter key.  Are these not the correct commands any more?
0
Maya
Telerik team
answered on 20 Feb 2015, 03:53 PM
Hello Koren,

It seems that there is a delay in committing the changes for the newly added item. What you can try is:
commandsToExecute.Clear();
            parentGrid.Items.CommitNew();
            commandsToExecute.Add(RadGridViewCommands.CommitEdit);
            commandsToExecute.Add(RadGridViewCommands.BeginInsert);




Regards,
Maya
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Koren
Top achievements
Rank 1
answered on 20 Feb 2015, 04:10 PM
That doesn't work.  It just leaves me with what looks like an empty record at the bottom and doesn't go into insert mode still.  If I go away from that tab and come back, it looks like my record was actually committed but it doesn't look that way during entry.

My keyboard command provider is set up to keep the grid in insert mode for either the tab or enter key.  So when I hit the last column it should commit the edit and then begin insert.  After the edit is committed, I can see that BeginInsert is in the PendingCommands but it never happens.
​
0
Maya
Telerik team
answered on 23 Feb 2015, 11:53 AM
Hi Koren,

I tested the code and it seems to work correctly on my side. Could you check out the project attached ? Do you get the expected behavior on it ?

Regards,
Maya
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Koren
Top achievements
Rank 1
answered on 23 Feb 2015, 04:03 PM
I will open a ticket with the updated project including my results.
0
Maya
Telerik team
answered on 24 Feb 2015, 04:08 PM
Hello Koren,

The issue appears as NewRowPosition is set. I resolved the issue and it will be available in one of our next internal builds. 
Thanks for sending the project and the cooperation. I updated your Telerik points.

Regards,
Maya
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
GridView
Asked by
Koren
Top achievements
Rank 1
Answers by
Maya
Telerik team
Koren
Top achievements
Rank 1
Share this question
or