如何让按 Enter 键导致焦点移动到下面的单元格,就像 Excel 的默认行为一样?
我正在使用 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();
相反,会抛出异常:
类型异常 '系统.IndexOutOfRangeException' 发生在 Infragistics2.Shared.v9.1.dll 但被 未在用户代码中处理 信息:指数超出范围 数组的边界。
由于我位于第 0 行,并且存在第 1 行,这让我感到惊讶。 currentRow 和 col 的值分别为 0 和 28。什么是更好的方法? 顺便说一句,我可以在下面的单元格中再次执行此操作,其中值是 currentRow = 1 和 col = 28。抛出相同的异常。
I am using an Infragistics UltraWinGrid v9.1.
I want to allow the user to enter numerical data in a cell, press Enter and then have the focus on the cell below, like you see in Excel. It appears that the KeyUp event might be better than the KeyPressed event for this, but I keep throwing an exception that I have gone beyond the bounds of the UltraWinGrid even though I start at the top of a full grid. Here is the code I've tried:
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();
}
}
I expected this to make the cell in the same column but one row below to become the active cell with the call,
grid.Rows[currentRow + 1].Cells[grid.ActiveCell].Activate();
Instead an exception is thrown:
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.
Since I'm on row 0 and there exists a row 1 this is a surprise to me. The values for currentRow and col are 0 and 28 respectively. What would be a better approach?
btw I can do this again the cell below, where the values are currentRow = 1 and col = 28. The same exception is thrown.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有人在 Infragistics 论坛上回答了我的问题...
Someone answered my question on the Infragistics fora...
我不知道我所说的是否也适用于 v9.1,但你也可以这样做:
I don't know if what I'm saying is valid also for the v9.1 but you can also do something like this:
使用 grid.PerformAction(UltraGridAction.BelowCell) 后,活动行将发生变化,但下一个单元格不处于编辑模式。
After using
grid.PerformAction(UltraGridAction.BelowCell)
, active row will change but next cell is not be in edit mode.