0

我有两种形式:

  1. 编辑

在“编辑器”表单上,我有一个“KeyDown”事件,当 F5 键按下时会打开“表格”表单。

    Private Sub DataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView.KeyDown
    Select Case e.KeyCode
        Case Keys.Delete
            For Each Row As DataGridViewRow In DataGridView.Rows
                If Row.IsNewRow Then
                    DataGridView.Rows.Remove(Row)
                End If
            Next
        Case Keys.F5
            Tabels.Show()
    End Select
End Sub

在“表”表单上,我有一些代码,它是一个 List(Of String) 并循环遍历它,将行添加到表单上的 datagridview 中。

        Dim Tables As List(Of String)
    Tables = NAV2013BLOBReader.ReturnXML("SELECT [Metadata] FROM dbo.[Object Metadata] WHERE [Object Type] = 1")

    'Sæt egenskaber på datagridview
    DataGridView.AutoGenerateColumns = False

    Dim Row As DataGridViewRow = DataGridView.Rows(0).Clone()
    For Each Line As String In Tables
        If Line.Contains("MetaTable") Then
            Dim LanguageStartIndex As Integer = Line.IndexOf("CaptionML=""") + 15
            Dim LanguageEndIndex As Integer = Line.IndexOf(";", LanguageStartIndex)

            Dim IdStartIndex As Integer = Line.IndexOf("ID=""") + 4
            Dim IdEndIndex As Integer = Line.IndexOf(""" CaptionML") - IdStartIndex

            Dim NameStartIndex As Integer = Line.IndexOf("Name=""") + 6
            Dim NameEndIndex As Integer = Line.IndexOf(""" LookupFormID") - NameStartIndex

            'Tildel cellerne værdier
            Row.Cells(0).Value = Line.Substring(IdStartIndex, IdEndIndex)
            Row.Cells(1).Value = Line.Substring(NameStartIndex, NameEndIndex)
            Row.Cells(2).Value = Line.Substring(LanguageStartIndex, LanguageEndIndex - LanguageStartIndex)

            DataGridView.Rows.Add(Row)
            DataGridView.Refresh()
        End If
    Next

    DataGridView.AllowUserToAddRows = False

问题是当循环迭代两次时,流程将退出循环并返回到 KeyDown 事件,然后该事件结束。

如果我清空If Line.Contains语句中的代码,并插入一个 MessageBox,那么一切都会很好地迭代。

我希望有人知道答案。

谢谢和最好的问候!

编辑:我可以看到这DataGridView.Rows.Add(Row)是错误的,但我不知道为什么。

编辑:我通过将“For Each”循环更改为“For”循环并克隆最新添加的行来修复它。

For i = 0 To Tables.Count - 1
        Dim Row As DataGridViewRow = DataGridView.Rows(i).Clone()
        If Tables(i).Contains("MetaTable") Then
            Dim LanguageStartIndex As Integer = Tables(i).IndexOf("CaptionML=""") + 15
            Dim LanguageEndIndex As Integer = Tables(i).IndexOf(";", LanguageStartIndex)

            Dim IdStartIndex As Integer = Tables(i).IndexOf("ID=""") + 4
            Dim IdEndIndex As Integer = Tables(i).IndexOf(""" CaptionML") - IdStartIndex

            Dim NameStartIndex As Integer = Tables(i).IndexOf("Name=""") + 6
            Dim NameEndIndex As Integer = Tables(i).IndexOf("""", Tables(i).IndexOf("Name=""") + 6) - NameStartIndex

            'Tildel cellerne værdier
            Row.Cells(0).Value = Tables(i).Substring(IdStartIndex, IdEndIndex)
            Row.Cells(1).Value = Tables(i).Substring(NameStartIndex, NameEndIndex)
            Row.Cells(2).Value = Tables(i).Substring(LanguageStartIndex, LanguageEndIndex - LanguageStartIndex)

            'Tilføj række til DataGridView og opfrisk kontrollen, så at de nye rækker bliver vist med det samme
            DataGridView.Rows.Add(Row)
            DataGridView.Refresh()
        End If
    Next
4

1 回答 1

0

编辑:我通过将“For Each”循环更改为“For”循环并克隆最新添加的行来修复它。

For i = 0 To Tables.Count - 1
    Dim Row As DataGridViewRow = DataGridView.Rows(i).Clone()
    If Tables(i).Contains("MetaTable") Then
        Dim LanguageStartIndex As Integer = Tables(i).IndexOf("CaptionML=""") + 15
        Dim LanguageEndIndex As Integer = Tables(i).IndexOf(";", LanguageStartIndex)

        Dim IdStartIndex As Integer = Tables(i).IndexOf("ID=""") + 4
        Dim IdEndIndex As Integer = Tables(i).IndexOf(""" CaptionML") - IdStartIndex

        Dim NameStartIndex As Integer = Tables(i).IndexOf("Name=""") + 6
        Dim NameEndIndex As Integer = Tables(i).IndexOf("""", Tables(i).IndexOf("Name=""") + 6) - NameStartIndex

        'Tildel cellerne værdier
        Row.Cells(0).Value = Tables(i).Substring(IdStartIndex, IdEndIndex)
        Row.Cells(1).Value = Tables(i).Substring(NameStartIndex, NameEndIndex)
        Row.Cells(2).Value = Tables(i).Substring(LanguageStartIndex, LanguageEndIndex - LanguageStartIndex)

        'Tilføj række til DataGridView og opfrisk kontrollen, så at de nye rækker bliver vist med det samme
        DataGridView.Rows.Add(Row)
        DataGridView.Refresh()
    End If
Next
于 2015-02-23T11:32:16.390 回答