DataGridView CurrentRow 排除新行
我想获取 DataGridView 当前行的索引。 问题是,如果当前行是新行,则 CurrentRow 将设置为不是新行的最后一行。 我无法检查要选择的行,因为如果选择了一行,并不意味着它是当前行,并且不一定选择当前行。
我可以获得新行的索引,但如何知道新行是否是当前行?
I want to get the index of the current row of my DataGridView. The problem is, that if the current row is the new row, CurrentRow is set to the last row that is not the new row. I cannot check for the rows to be selected because if a row is selected that doesn't mean it is the current row and the current row isn't necessarily selected.
I can get the index of the new row, but how can I know whether the new row is the current row?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个房产价值能解决您的问题吗?
Can this property value solve your problem?
我使用 CellEnter 事件将 CurrentCell.RowIndex 保存到表单变量中。 单击另一个按钮会导致此事件以最后一个现有行索引触发,而不是我们需要的新行索引,因此测试焦点以忽略这种情况。
这避免了像使用 CellClick 事件那样检查可能将光标移动到新行的所有可能的按键和鼠标操作。
从 VB 自动转换的代码:
I use the CellEnter event to save CurrentCell.RowIndex into a form variable. Clicking another button causes this event to fire with the last existing row index instead of the new row index that we need so test for focus to ignore that case.
This avoids having to check for all possible key and mouse actions that might move the cursor to the new row as you do with the CellClick event.
Code auto-converted from VB:
我通过以下代码连接到 CellClick 事件:
当该条件为真时,用户“选择”新行。 根据该信息,您可能需要实际插入一条新记录(使用 ..Rows.Add() 方法),然后才能实际对其执行任何操作,因为新行实际上并不存在于集合中; 它只是作为占位符来表示新行。
I hooked into the CellClick event via the following code:
When that condition is true, the user 'selected' the new row. Based upon that information, you may need to actually insert a new record (using the ..Rows.Add() method) before you can actually do anything with it since the new row doesn't actually exist in the collection; it's just there as a placeholder to represent a new row.