DataGridView:设置CurrentCell设置选中,不移动光标。 [英] DataGridView: Setting CurrentCell sets selected, does not move cursor.

查看:295
本文介绍了DataGridView:设置CurrentCell设置选中,不移动光标。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

        /// <summary>
        /// This happens AFTER the rows have been added, but BEFORE the CurrentRow has been changed.  Therefore you can't use the currentRow to
        /// find out where you are pointing.  The e.Rowindex is WRONG when you add rows programmatically.  Had to use ROWCOUNT instead.
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void purchase_Order_DetailDataGridView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            if (!isFetchingData)
            {
                DataGridView Grid = (sender as DataGridView);
                int lastrow = Grid.RowCount-1;

                if (lastrow > 0)
                {                    
                    DataGridViewCell Cell= Grid.Rows[lastrow].Cells["Pick"];                                        
                    Grid.CurrentCell = Grid.Rows[lastrow].Cells["Pick"];
                    Grid.BeginEdit(true);
                }
            }
        }

结果:

似乎正确的单元格是"已选择"。但是光标位于上一行,

It seems the correct cell is "Selected" but the cursor is in the previous row,

因此,有2行,0和1,1应该是"当前"。 调试器确认。

So, there are 2 rows, 0 and 1, and 1 should be "Current".  The debugger confirms.

然而,"光标"是指光标。仍然在第0行。

Yet the "Cursor" is still in Row 0.

我尝试在各个地方刷新网格以使效果无效。

I have tried refreshing the grid at various places to null effect.

所需的行为是将光标放在"Pick"中的新行中。添加后的列(按下添加行按钮)。

The desired behavior is to have the cursor in the new row in the "Pick" column after an add (Add row button pressed).

我宁愿生活在虚假的希望之中虚假的绝望。

I'd rather live with false hope than with false despair.

推荐答案

嗨布莱恩,

看来我无法重现这个问题,在我的代码测试中,光标正确地转到新行:

It seems I can not reproduce this issue, in my test with your code, the cursor goes to the new row correctly:

可能问题是由于项目中的其他代码造成的,请检查一下。

Maybe the problem is due to other code in your project, please check it.

问候,

Stanly


这篇关于DataGridView:设置CurrentCell设置选中,不移动光标。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆