首页 > 解决方案 > DataGridView 选定的行索引

问题描述

为什么 dgView.CurrentCell.RowIndex 总是 0?我需要知道用户正在尝试编辑哪一行和哪一列。只有当它是新行时,才能编辑第 1 列。只有当它不是新行时,才能编辑第 4 列。不能编辑其他列。我需要能够添加一个新行。我已经尝试将 .CellBeginEdit 与 e 作为 DataGridViewCellCancelEventArgs

不,我不想将我的数据绑定到 SQL 表。我需要确保用户知道他们在保存数据时正在更改数据。

我已经尝试过使用 CellMouseClick 和 CellMouseLeeave,但结果是灾难性的。我找到了这两个,并认为我让它们工作,但测试证明并非如此。我的代码(其中一些是多余的,因为我仍在尝试了解我应该使用哪些事件):

Private Sub dgTools_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles dgTools.CellBeginEdit
    Dim i As Integer
    With dgTools
        i = .CurrentCell.RowIndex
        Select Case e.ColumnIndex
            Case 1
                If .CurrentCell.RowIndex < .NewRowIndex Then
                    Dim msg As Object, hdr As Object
                    msg = "You may not edit the tool ID"
                    hdr = "Edit Error"
                    MsgBox(msg, MsgBoxStyle.Critical, hdr)
                End If
            Case 4
                originalValue = .CurrentCell.Value
        End Select
        .EndEdit()
    End With
End Sub

Private Sub dgTools_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles dgTools.CellEndEdit
    Dim dtCheck As New DataTable
    Dim hdr As Object, msg As Object

    With dgTools
        .EndEdit()
        Select Case e.ColumnIndex
            Case 1
                Select Case originalValue
                    Case ""
                        .CurrentCell.Value = UCase(.CurrentCell.Value)
                        dtCheck = GetDataTable("GetTool_x_BrochsteinsID", "@BrochsteinsID", .CurrentCell.Value)
                        ' Brochsteins ID is not a valid ID
                        If dtCheck.Rows.Count = 0 Then
                            hdr = "Invalid Brochsteins ID"
                            msg = "Entry is not a valid Brochsteins ID." & vbCrLf & "Please enter a valid ID."
                            MsgBox(msg, MsgBoxStyle.Critical, hdr)
                            .Rows.RemoveAt(e.RowIndex)
                            Exit Sub
                        End If
                        ' Check that Brochsteins ID is not already assigned to a toolbox
                        If dtCheck.Rows(0)("Toolstatus") > 1 Then
                            hdr = "Illegal Brochsteins ID Assignment"
                            msg = dtCheck.Rows(0)("BrochsteinsID") & " is already assigned to " & dtCheck.Rows(0)("ToolboxDesc") & vbCrLf &
                                      "Please enter a different Brochsteins ID or Return this ID to the Tool Room first."
                            MsgBox(msg, MsgBoxStyle.Critical, hdr)
                            .Rows.RemoveAt(e.RowIndex)
                            Exit Sub
                        End If
                        .CurrentRow.Cells(0).Value = dtCheck.Rows(0)("ToolId")
                        .CurrentRow.Cells(2).Value = dtCheck.Rows(0)("ModelDesc").ToString
                        .CurrentRow.Cells(3).Value = dtCheck.Rows(0)("CategoryDesc").ToString
                        .CurrentRow.Cells(4).Value = dtCheck.Rows(0)("AssignedNote").ToString
                    Case Else
                        If originalValue <> .CurrentCell.Value Then
                            hdr = "Edit Brochsteins ID"
                            msg = "You may not edit a Brochsteins ID." & vbCrLf & "You may only Delete and/or Add a new Brochsteins ID."
                            MsgBox(msg, MsgBoxStyle.Critical, hdr)
                            .CurrentCell.Value = originalValue
                            Exit Sub
                        End If
                End Select
        End Select
        Dim Note As String
        If Not IsDBNull(.CurrentRow.Cells("AssignedNote").Value) Then Note = .CurrentRow.Cells("AssignedNote").Value Else Note = ""
        UpdateTool_Toolbox(.CurrentRow.Cells("ToolID").Value, cboToolbox.SelectedValue, Note)
        If dtCheck IsNot Nothing Then dtCheck.Dispose()
        dgTools.EndEdit()
        QueryTable("Tools")
        QueryTable("Standard")
    End With

End Sub

标签: vb.netdatagridview

解决方案


推荐阅读