0

私が抱えている問題について助けを求めています。フォルダーに複数のテキスト ファイルがあります。フォルダーには「無制限」の量のテキスト ファイルを含めることができますが、通常は 2 ~ 150 個のファイル http://gyazo.com/5f314d1ca374abf9f813914609dd931d (この画像と以下の画像、評判が悪いため埋め込みできません)

各ファイルには、「無制限」(通常は 0 ~ 20 行ですが) の量のデータが含まれます。行 1 は「テスト番号」、行 2 は「テスト結果」です - 上の画像に見られるように

私のデータ グリッド ビューには 3 つの列 (ユーザー名、テスト番号、テスト結果) があります - 上の画像に見られるように

以下のコードを実行するとき; Data Grid View に追加された最初のレコードは完全に機能しますが、2 番目 (または最初のレコード以降の任意のレコード) でエラーが発生します: http://gyazo.com/0d492b97d918853e62c55ee314f3f181 (画像) またはエラー メッセージ:

「System.InvalidOperationException: コレクションは既に DataGridView コントロールに属しています。この操作は有効ではありません。System.Windows.Forms.DataGridViewCellCollection.Add(DataGridViewCelldataGridViewCell) で SpellingBee.TeacherMultiResults.TeacherMultiResults_Load(オブジェクト送信者、EventArgs e)」

このエラーは、DGVRow.Cells.Add(DGVCell) 'add cell to row

私がやりたくないことの 1 つは、現在のストレージが非効率的であることを認識していても、テキスト ファイル/フォルダー構造を変更することです。

このエラーを修正するにはどうすればよいですか? 私はVB.net(Visual Studios 2013)を使用しています

さらに情報が必要な場合は、お問い合わせください

どうもありがとう。

Imports System.Windows.Forms
Imports System.IO

Public Class TeacherMultiResults

    Dim AmountOfFiles
    Dim LineCount As Integer = 0
    Dim FileName As IO.FileInfo
    Dim DGVRow As New DataGridViewRow
    Dim DGVCell As DataGridViewCell
    Dim Username As String = ""
    Dim TestNumber As Integer = 0
    Dim TestResult As Integer = 0

    Private Sub TeacherMultiResults_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim directory As New IO.DirectoryInfo(LoadForm.CurrentDirectory & "\UserResults\") 'selects directory
        Dim FileNames As IO.FileInfo() = directory.GetFiles()
        Dim Files As IO.FileInfo

        For Each Files In FileNames 'list the names of all files in the specified directory
            Username = Files.ToString
            Username = (Username.Substring(0, Username.Length - 4)) 'removes the .txt from the name

            Try
                LineCount = File.ReadAllLines(LoadForm.CurrentDirectory & "\UserResults\" & Username & ".txt").Length 'amount of lines in file
                If LineCount > 1 Then
                    Dim Information As New System.IO.StreamReader(LoadForm.CurrentDirectory & "\UserResults\" & Username & ".txt") 'opens file
                    LineCount = LineCount / 2 'halfs line count
                    For i = 0 To LineCount - 1
                        TestNumber = Information.ReadLine() 'reads line to variable
                        TestResult = Information.ReadLine() 'reads line to variable
                        i = i + 1 'adds one to i

                        DGVCell = New DataGridViewTextBoxCell 'create a new DGV text box cell
                        DGVCell.Value = Username 'add value to DGV text box cell
                        DGVRow.Cells.Add(DGVCell) 'add cell to row

                        DGVCell = New DataGridViewTextBoxCell 'create a new DGV text box cell
                        DGVCell.Value = TestNumber 'add value to DGV text box cell
                        DGVRow.Cells.Add(DGVCell) 'add cell to row

                        DGVCell = New DataGridViewTextBoxCell 'create a new DGV text box cell
                        DGVCell.Value = TestResult 'add value to DGV text box cell
                        DGVRow.Cells.Add(DGVCell) 'add cell to row

                        DGV_MultiResults.Rows.Add(DGVRow) 'add row to DGV

                    Next
                    Information.Close() 'Close read
                End If

            Catch ex As Exception 'if file won't read
                MsgBox(ex.ToString) 'show error
                Exit Try
            End Try 'end of try

        Next 'end of files

    End Sub

誰かがこの問題で私を助けてくれることを願っています.エラーの解決策を見つけることができないようですが、おそらく修正するのはかなり簡単だと思います.

どうもありがとう、トビー。

4

1 に答える 1