在 DataGridView 中选择新行的第一个可见单元格

发布于 2024-10-05 18:27:55 字数 1191 浏览 0 评论 0原文

我试图集中输入并在添加到表单中 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”是一个隐藏单元格,用于存储行的自定义状态。

在添加行之前,我们在表单上看到的是: Before we add a new row.

这是我们实际尝试添加新行时看到的内容: 单击行添加按钮。

请注意,列 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:
Before we add a new row.

This is what we see when we actually try and add a new row:
Row Add button clicked.

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:
Desired outcome from clicking the 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 技术交流群。

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

发布评论

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

评论(1

微暖i 2024-10-12 18:27:55

不要使用 DataGridView 的 RowAdded 事件来设置 CurrentCell,而是在向 DGV 添加新记录的任何位置添加以下代码(我假设在“添加”按钮的 Click 事件中):

''# Add the new record to your Data source/DGV.

For Each row As DataGridViewRow In grd_GoldAdders.Rows
    If row.Cells("grid_flag").FormattedValue = Constants.[New] Then 
        grd_GoldAdders.CurrentCell = row.Cells("AssySiteColumn")  ''# I'm calling the first column in your DGV 'AssySiteColumn'.
        grd_GoldAdders.BeginEdit(False)
        Exit For
    End If
Next

此代码只是循环遍历 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):

''# Add the new record to your Data source/DGV.

For Each row As DataGridViewRow In grd_GoldAdders.Rows
    If row.Cells("grid_flag").FormattedValue = Constants.[New] Then 
        grd_GoldAdders.CurrentCell = row.Cells("AssySiteColumn")  ''# I'm calling the first column in your DGV 'AssySiteColumn'.
        grd_GoldAdders.BeginEdit(False)
        Exit For
    End If
Next

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.

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