在 DataGridView 中选择新行的第一个可见单元格
我试图集中输入并在添加到表单中 DataGridView 的每个新行上触发编辑事件。
这是我试图用户实现此目的的代码。
Private Sub grd_GoldAdders_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles grd_GoldAdders.RowsAdded
Dim grid As DataGridView = DirectCast(sender, DataGridView)
grid.ClearSelection()
If grid.Rows(e.RowIndex).Cells("grid_flag").FormattedValue = Constants.[New] Then
For Each cell As DataGridViewCell In grid.Rows(e.RowIndex).Cells
If Not cell.Visible Then Continue For
grid.CurrentCell = cell
grid.BeginEdit(False)
Exit For
Next
End If
End Sub
“grid_flag”是一个隐藏单元格,用于存储行的自定义状态。
在添加行之前,我们在表单上看到的是:
这是我们实际尝试添加新行时看到的内容:
请注意,列 0,0 和新行的第一个可见列均已选中,但是0,0 列具有焦点。我不希望 0,0 被选中或获得焦点。我还在这里看到行指示器也指向第 0 行...
这就是我希望在单击“添加”按钮后看到的内容:
有谁知道我的代码哪里出了问题?我一天的大部分时间都在寻找解决这个问题的方法。
I'm trying to focus input and fire the editing event on each new row that I add to a DataGridView in my form.
This is the code I am trying to user to achieve this.
Private Sub grd_GoldAdders_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles grd_GoldAdders.RowsAdded
Dim grid As DataGridView = DirectCast(sender, DataGridView)
grid.ClearSelection()
If grid.Rows(e.RowIndex).Cells("grid_flag").FormattedValue = Constants.[New] Then
For Each cell As DataGridViewCell In grid.Rows(e.RowIndex).Cells
If Not cell.Visible Then Continue For
grid.CurrentCell = cell
grid.BeginEdit(False)
Exit For
Next
End If
End Sub
The "grid_flag" is a hidden cell which is used to store custom states for a row.
Prior to adding a row, this is what we see on the form:
This is what we see when we actually try and add a new row:
Notice that both the column 0,0 and the first visible column of the new row are selected, but the column 0,0 has the focus. I do not wish for 0,0 to either get selected or have the focus. I also see here that the row indicator is pointing at row 0 too...
This is how I would like to see things after clicking my Add button:
Does anyone know where I am going wrong with the code? I've searched SO for the most part of the day trying to solve this one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要使用 DataGridView 的 RowAdded 事件来设置 CurrentCell,而是在向 DGV 添加新记录的任何位置添加以下代码(我假设在“添加”按钮的 Click 事件中):
此代码只是循环遍历 DGV 中的所有行,并且使用
Constants.[New]
标志值将第一行中的第一个单元格指定为 CurrentCell。Instead of using your DataGridView's RowAdded event to set the CurrentCell, add the following code wherever you're adding a new record to your DGV (in your Add button's Click event I assume):
This code simply loops through all the rows in your DGV and specifies as the CurrentCell the first cell in the first row with your
Constants.[New]
flag value.