如何让按 Enter 键导致焦点移动到下面的单元格,就像 Excel 的默认行为一样?

发布于 2024-08-21 03:45:05 字数 1071 浏览 7 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

乖乖公主 2024-08-28 03:45:05

有人在 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);
        }
    }

Someone answered my question on the Infragistics fora...

    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);
        }
    }
み零 2024-08-28 03:45:05

我不知道我所说的是否也适用于 v9.1,但你也可以这样做:

yourGrid.KeyActionMappings.Add(new GridKeyActionMapping(Keys.Enter, UltraGridAction.BelowCell, 0, UltraGridState.Row, SpecialKeys.All, 0));

I don't know if what I'm saying is valid also for the v9.1 but you can also do something like this:

yourGrid.KeyActionMappings.Add(new GridKeyActionMapping(Keys.Enter, UltraGridAction.BelowCell, 0, UltraGridState.Row, SpecialKeys.All, 0));
谁的年少不轻狂 2024-08-28 03:45:05
 private void ulGrvProducts_KeyUp(object sender, KeyEventArgs e)
        {

            UltraGrid grid =  (UltraGrid)sender;

            if (e.KeyCode == Keys.Enter)
            {
                 //Go down one row
                grid.PerformAction(UltraGridAction.BelowCell);
            }
        }
 private void ulGrvProducts_KeyUp(object sender, KeyEventArgs e)
        {

            UltraGrid grid =  (UltraGrid)sender;

            if (e.KeyCode == Keys.Enter)
            {
                 //Go down one row
                grid.PerformAction(UltraGridAction.BelowCell);
            }
        }
橘香 2024-08-28 03:45:05

使用 grid.PerformAction(UltraGridAction.BelowCell) 后,活动行将发生变化,但下一个单元格不处于编辑模式。

After using grid.PerformAction(UltraGridAction.BelowCell) , active row will change but next cell is not be in edit mode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文