In my project, I need to move a cell in edit mode in the ultragrid to the same cell (column) in next row when the tab key is pressed. I did the following in the form_load event.
foreach (GridKeyActionMapping mapping in this.ultraGrid1.KeyActionMappings) {
{
if (mapping.KeyCode == Keys.Tab && mapping.SpecialKeysDisallowed == SpecialKeys.All) {mapping.ActionCode =
{mapping.ActionCode =
UltraGridAction.BelowCell; mapping.StateRequired =
mapping.StateRequired =
UltraGridState.InEdit;}
Unfortunately, when I run it, the tab doesn't move at all.
I also tried adding new GridKeyActionMapping with similar settings in the form_load event. When tab is pressed, it only move to the next cell without going down to next row as I expected.
Does anyone came across similar problem or have solution for it?
Thanks in advance!
Henry
One thing I would recommend is to remove the existing tab key mappings rather than try to modify them. I listed an example of how to do this below, although it is not a complete solution...
...because there is one problem with this that I don't think can be overcome without handling events, and that is the fact that you can't navigate to the below cell and enter edit mode on it using only one key mapping. So this will navigate to the below cell if the currently active cell is in edit mode, but it will not enter edit mode because it requires a second mapping to do that (the 'NextCellByTab' action does both).
You should be able to get around this by handling AfterCellActivate and placing the cell in edit mode, but one side effect of this is that the cell will always go into edit mode upon activation, which you might not want.
Example:List<GridKeyActionMapping> mappingsToRemove = new List<GridKeyActionMapping>();foreach( GridKeyActionMapping mapping in this.ultraGrid.KeyActionMappings ){ if ( (mapping.KeyCode & Keys.Tab) == Keys.Tab ) mappingsToRemove.Add( mapping );}
foreach( GridKeyActionMapping mapping in mappingsToRemove ){ this.ultraGrid.KeyActionMappings.Remove( mapping );}
this.ultraGrid.KeyActionMappings.Add( new GridKeyActionMapping( Keys.Tab, UltraGridAction.BelowCell, 0, UltraGridState.InEdit, SpecialKeys.All, 0) );
Thanks Brain for the prompt reply and recommendation.
I put the code in the form_load. Unfortunately, it doesn't move to either next cell or the cell in the next row when I press the tab key. I have tried to use nextcellbytab, it moves the focus on next cell but not the cell in next row. The version of UltraWinGrid is Infragistics2.Win.UltraWinGrid.v9.1. I use Visual Studio 2008 for development.
One thing I noticed that when I press the shift+tab key, it move the focus out of the ultrawingrid. I guess it is caused by all tab related keys are being removed in the code. It is our the requirement that shift +tab key should move it to the cell in previous row.
Any help on this will be grateful.
Personally, I find that using KeyActionMappings to modify the grid behavior is usually more complicated than you really need. Also, BelowCell and AboveCell don't appear to work the way you would expect when using RowLayouts, so that might be why this isn't working for you.
Here's some code that worked for me both in RowLayout mode and the standard mode:
private void ultraGrid1_BeforePerformAction(object sender, BeforeUltraGridPerformActionEventArgs e) { UltraGrid grid = (UltraGrid)sender; switch (e.UltraGridAction) { case UltraGridAction.NextCellByTab: e.Cancel = true; if (grid.ActiveRow.HasNextSibling()) { UltraGridRow nextRow = grid.ActiveRow.GetSibling(SiblingRow.Next); grid.ActiveCell = nextRow.Cells[grid.ActiveCell.Column]; } break; case UltraGridAction.PrevCellByTab: e.Cancel = true; if (grid.ActiveRow.HasPrevSibling()) { UltraGridRow prevRow = grid.ActiveRow.GetSibling(SiblingRow.Previous); grid.ActiveCell = prevRow.Cells[grid.ActiveCell.Column]; } break; } }
Thanks a lot for the reply. This works for textbox. However, when there is a boolean field with checkbox in the UI, it sometimes doesn't get to the last row. Also I tried to use space key to check/uncheck the checkbox. It doesn't work. Any idea how to deal with this problem?
henryzchen said:it sometimes doesn't get to the last row.
Not sure what you mean by this. I tried it out and it works fine for me.
henryzchen said:Also I tried to use space key to check/uncheck the checkbox. It doesn't work. Any idea how to deal with this problem?
The spacebar doesn't work because the cell is not in edit mode. So to fix that, after you set the ActiveCell, call PerformAction(EnterEditMode).