After editing a cell in a grid, the next row below is automatically selected. But if the grid is sorted, and the edit makes the current row to be moved, the row which is selected after the edit is the row below the new position. I would expect that the row to be selected is the one below the original location.
I've attached an image from a test application, and when comitting the active edit the row will be moved upwards (due to the sorting), and the next row to be selected is the one with IntegerValue 16. I want it to be the row with IntegerValue 32. Almost feels like a bug?
I've tried to override this by storing the selected row when the CellEditEnded-event is triggerd, and then trying to select it at SelectionChanging or SelectionChanged, but my attempts fail.
4 Answers, 1 is accepted
With the current implementation of the control's sorting and editing functionality, such a behavior is expected. To achieve the desired result, you can define a custom KeyboardCommandProvider and define its ProvideCommandsForKey override like so:
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();
var currentIndex =
this
.parentGrid.Items.IndexOf(
this
.parentGrid.SelectedItem);
commandsToExecute.Add(RadGridViewCommands.CommitEdit);
this
.parentGrid.SelectedItem =
this
.parentGrid.Items[currentIndex + 1];
}
return
commandsToExecute;
}
}
this
.Grid.KeyboardCommandProvider =
new
CustomKeyboardCommandProvider(
this
.Grid);
Please let me know whether this would work for you.
Regards,
Dilyan Traykov
Progress Telerik
Well, the selection works, but it seems like the keyboard (and sometimes mouse) gets hijacked after the CustomKeyboardCommandProvider is excuted. The original commands on Enter are CommitEdit, MoveDown, SelectCurrentUnit, BeginEdit.
- If I just do CommitEdit and my owrn selection (this.parentGrid.SelectedItem = whatever), the selection works but it's clearly selecting some other row first, and then jumping to my selected row. And the next row isn't set into edit mode.
- If I just replace MoveDown with my own code selection works, but no new edit is started, and I cannot start any new edit by mouse or keyboard.
- I also tried not executing SelectCurrentUnit, and then I could start a new edit by mouseclick, but I couldn't enter anything because I got no reaction from the keyboard.
I don't know if it's something on our side that produces this behavior, I must continue to debug I guess.
Please excuse me for misleading you. I've omitted the following two lines from the previous code snippet:
if
(key == Key.Enter)
{
commandsToExecute.Clear();
var currentIndex =
this
.parentGrid.Items.IndexOf(
this
.parentGrid.SelectedItem);
commandsToExecute.Add(RadGridViewCommands.CommitEdit);
this
.parentGrid.SelectedItem =
this
.parentGrid.Items[currentIndex + 1];
this
.parentGrid.CurrentCellInfo =
new
GridViewCellInfo(
this
.parentGrid.SelectedItem,
this
.parentGrid.CurrentColumn);
this
.parentGrid.BeginEdit();
}
Adding those lines seem to provide the desired result at my end. I've attached a small sample project as a demonstration.
Please let me know if this works for you.
Regards,
Dilyan Traykov
Progress Telerik