我正在为我遇到的问题寻求帮助。 我在一个文件夹中有多个文本文件。该文件夹可以有一个"无限制"其中包含大量文本文件,但通常为2-150个文件 http://gyazo.com/5f314d1ca374abf9f813914609dd931d(此图片+以下图片,由于缺乏声誉而无法嵌入)
然后每个文件包含一个"无限制" (虽然通常为0-20行)其中的数据量。第1行是"测试号码"第2行是"测试结果" - 如上图所示
我的数据网格视图中有3列(用户名,测试编号,测试结果) - 如上图所示
在下面运行我的代码时;添加到数据网格视图的第一个记录完美地工作,但第二个(或第一个之后的任何记录)发生错误: http://gyazo.com/0d492b97d918853e62c55ee314f3f181(图片)或错误消息:
" System.InvalidOperationException:集合已经属于a DataGridView控件。此操作不再有效。在 System.Windows.Forms.DataGridViewCellCollection.Add(DataGridViewCelldataGridViewCell) 在SpellingBee.TeacherMultiResults.TeacherMultiResults_Load(对象 发件人,EventArgs e)"
DGVRow.Cells.Add(DGVCell) 'add cell to row
我不想做的一件事是改变我的任何文本文件/文件夹结构,即使我知道它目前是低效的存储空间。
如何修复此错误?我使用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
我希望有人可以帮助我解决这个问题,我知道解决这个问题相当简单,尽管我似乎找不到错误的解决方案!
非常感谢,托比。
答案 0 :(得分:0)
使用DataGridView时,通常更容易构建一个对象列表,然后使用内置的数据绑定功能将其绑定到DGV。
对于您的示例,它看起来像这样:
' Read the files, build objects as you go and add them to a list
Dim lst = New List(Of TeacherMultiResults)
' Replace this part with your file reading code...
Dim t1 = New TeacherMultiResults With {.Username = "Bob", .TestNumber = 1, .TestResult = 75}
lst.Add(t1)
Dim t2 = New TeacherMultiResults With {.Username = "Rick", .TestNumber = 1, .TestResult = 85}
lst.Add(t2)
Dim t3 = New TeacherMultiResults With {.Username = "Zoe", .TestNumber = 1, .TestResult = 95}
lst.Add(t3)
' Bind the list to the DataGridView
Dim bindList = New BindingList(Of TeacherMultiResults)(lst)
Dim bindSrc = New BindingSource
bindSrc.DataSource = bindList
grid.DataSource = bindSrc
您的网格应显示列表中的每个项目。