我使用的是 Infragistics UltraWinGrid v9.1。 我想让用户在单元格中输入数字数据,按 Enter,然后将焦点放在下面的单元格上,就像您在 Excel 中看到的那样。看起来 KeyUp 事件在这方面可能比 KeyPressed 事件更好,但我一直抛出一个异常,我已经超出了 UltraWinGrid 的范围,即使我从完整网格的顶部开始也是如此。这是我试过的代码:
private void ugrid_KeyUp(object sender, KeyEventArgs e)
{
UltraGrid grid = (UltraGrid)sender;
if (e.KeyCode == Keys.Enter)
{
// Go down one row
UltraGridCell cell = grid.ActiveCell;
int currentRow = grid.ActiveRow.Index;
int col = cell.Column.Index;
grid.Rows[currentRow + 1].Cells[grid.ActiveCell].Activate();
}
}
我希望这会使位于同一列但下方一行的单元格成为调用的事件单元格, grid.Rows[currentRow + 1].Cells[grid.ActiveCell].Activate();
而是抛出一个异常:
An exception of type 'System.IndexOutOfRangeException' occurred in Infragistics2.Shared.v9.1.dll but was not handled in user code Additional information: Index was outside the bounds of the array.
因为我在第 0 行并且存在第 1 行,这让我感到惊讶。 currentRow 和 col 的值分别为 0 和 28。什么是更好的方法? 顺便说一句,我可以在下面的单元格中再次执行此操作,其中值为 currentRow = 1 和 col = 28。抛出相同的异常。
最佳答案
有人在 Infragistics 论坛上回答了我的问题...
private void ugrid_KeyUp(object sender, KeyEventArgs e)
{
var grid = (UltraGrid)sender;
if (e.KeyCode == Keys.Enter)
{
// Go down one row
grid.PerformAction(UltraGridAction.BelowCell);
}
}
关于infragistics - 如何使按下 Enter 键导致焦点像 Excel 的默认行为一样移动到下面的单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2247979/