1

在带有 datagridview 的表单和带有不同部件号的下拉列表中,当我从键盘按下删除键时,dgv 中的选定行被删除,但第一行对 dgv 隐藏。我必须从下拉列表中手动重新选择 p/n 才能再次看到它。我在表单上也有一个删除按钮,但效果很好,并且使用相同的子 DeleteCurrentRow。这是键盘删除键的 KeyDown 处理程序:

Private Sub DGVCurrentPoints_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DGVCurrentPoints.KeyDown
    If e.KeyValue = Keys.Delete Then
        DeleteCurrentRow()
    End If
End Sub

如果我按下键盘上的删除键或表单上的 Delete 按钮,则会调用这个子 DeleteCurrentRow:

 Private Sub DeleteCurrentRow()
    If Trim(CollectionPntPNList.Text).Length = 0 OrElse (CurrentPriorPassRequirementId = 0) Then
        MessageBox.Show("Nothing to delete." & vbNewLine & vbNewLine & _
                        "Please select a Part Number from the drop-down list, then select any cell on the data grid view." & _
                        vbNewLine & vbNewLine & "When the Delete button is pressed, the highlighted row will be deleted.",
                        "Nothing to delete",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Information)
        Exit Sub
    End If


    Dim result = MessageBox.Show("Delete the highlighted record? (Yes/No)",
                        "Ok to delete?",
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Question)
    If result = vbNo Then
        Exit Sub
    Else
        CollectionPoints.sDeleteCollectionPointSet(CurrentPriorPassRequirementId)
        DisplayCurrentCollectionPoints()
        DGVCurrentPoints.Refresh()
    End If
End Sub

CollectionPoints 是一个类,而 DisplayCurrentCollectionPoints() 是一个本地/私有子,用于重新填充 dgv。这是 DisplayCurrentCollectionPoints():

Private Sub DisplayCurrentCollectionPoints()
    Dim objConn As New SqlConnection(DatabaseConnection.FISSQLConnectionString)
    Dim objCommand As SqlCommand = objConn.CreateCommand
    Dim strSQL As New StringBuilder
    DGVCurrentPoints.DataSource = Nothing

    strSQL.Append("SELECT ")
    strSQL.Append("     ppr.PriorPassRequirementId AS [Row ID], ")
    strSQL.Append("     RTRIM(LTRIM(cp1.Description)) AS Description1, ")
    strSQL.Append("     ppr.CollectionPointId AS Point1, ")
    strSQL.Append("     RTrim(LTrim(cp2.Description)) AS Description2, ")
    strSQL.Append("       ppr.ReqPassedCollectionPointId AS Point2, ")
    strSQL.Append("ppr.Enabled, ")
    strSQL.Append("cp1.CollectionStep AS Step1, ")
    strSQL.Append("cp2.CollectionStep AS Step2 ")
    strSQL.Append("FROM ")
    strSQL.Append("     PriorPassRequirements AS ppr INNER JOIN ")
    strSQL.Append("       CollectionPoints AS cp1 ON ppr.CollectionPointId = cp1.CollectionPointId INNER JOIN ")
    strSQL.Append("       Products ON cp1.ProductIdValue = Products.ProductId INNER JOIN ")
    strSQL.Append("       CollectionPoints AS cp2 ON Products.ProductId = cp2.ProductIdValue ")
    strSQL.Append("         AND ppr.ReqPassedCollectionPointId = cp2.CollectionPointId ")
    strSQL.Append("WHERE ")
    strSQL.Append("     (ppr.CollectionPointId IN ")
    strSQL.Append("             (SELECT     cp.CollectionPointId ")
    strSQL.Append("             FROM ")
    strSQL.Append("                 CollectionPoints AS cp INNER JOIN ")
    strSQL.Append("                 Products AS p ON cp.ProductIdValue = p.ProductId ")
    strSQL.Append("             WHERE ")
    strSQL.Append("                 (p.PartNumber = N'" & Trim(CollectionPntPNList.Text) & "'))) ")
    strSQL.Append("ORDER BY Step1, Step2")
    objCommand.CommandText = strSQL.ToString

    Dim dtCurrPnts As New DataTable
    objConn.Open()
    dtCurrPnts.Load(objCommand.ExecuteReader)
    objConn.Dispose()
    With DGVCurrentPoints
        .DataSource = dtCurrPnts
        .AutoSizeColumnsMode = DataGridViewAutoSizeColumnMode.Fill
        .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        .Columns("Row ID").Visible = True
        .Columns("Description1").ReadOnly = True
        .Columns("Point1").ReadOnly = True
        .Columns("Description2").ReadOnly = True
        .Columns("Point2").ReadOnly = True
        .Columns("Enabled").ReadOnly = False
    End With
End Sub

在 DGVCurrentPoints_KeyDown 事件的 End Sub 之后,dgv 中的第一行被隐藏但没有被删除。这是我必须从下拉列表中重新选择 p/n 的地方。当我使用键盘删除键时,什么会导致第一行被隐藏?我使用 dgv.Refresh() 但这似乎不起作用。


根据 Plutonix 的建议,我添加了这个子来处理删除:

    Private Sub DGVCurrentPoints_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DGVCurrentPoints.KeyDown
    If e.KeyValue = Keys.Delete Then
        e.Handled = True
        DeleteCurrentRow()
        Exit Sub
    End If
End Sub
4

1 回答 1

3

DGV 已经可以响应 Delete 键删除选定/当前行。因此,当用户按 Delete 时,DGV 删除一个,而您删除一个。这也是按钮按预期工作的原因。这AllowUserToDeleteRows将防止这种情况发生,因此您可以在代码中完成所有操作,或添加以下内容:

If e.KeyValue = Keys.Delete Then 
    e.Handled = True         ' add
    DeleteCurrentRow()
End If

这应该可以防止 DGV 获得按键

于 2013-10-29T16:41:53.720 回答