Adding records to Data Grid View from text files. No text adding to DGV with lists VB.net

自闭症网瘾萝莉.ら 提交于 2019-12-13 07:01:41

问题


I'm looking for some help with an issue I am having. I have multiple text files in a folder. The folder can have an "unlimited" amount of text files in it, although typically 2-150 files.

http://gyazo.com/5f314d1ca374abf9f813914609dd931d (images for this + below, can't embed due to lack of reputation)

Each file then contains an "unlimited" (although typically 0-20 lines) amount of data inside it. Line 1 being the "test number" and line 2 being the "test result" - as seen in the image above

My Data Grid View has 3 columns in it [Username, Test Number and Test Result] named (Column_Username, Column_TestNumber, Column_TestResult) - as seen in the image above

When running the current code that I have, there is the correct amount of grids in the data grid view, although all of the cells are empty. http://gyazo.com/e08f9f2f4ab3971695feffe60503e8a9 (image of this)

One thing I don't want to do is change any of my text file/ folder structures, even though I'm aware that it is currently inefficient storage. Although I'm more than happy to use something instead of the lists function if you think there is a better way.

How can I fix this? I'm using VB.Net (visual studios 2013)

If you need any more information, please just ask.

Many thanks.

Imports System.IO
Imports System.ComponentModel

Public Class TeacherMultiResults

Dim LineCount As Integer = 0
Dim Username As String = ""
Dim TestNumber As Integer = 0
Dim TestResult As Integer = 0
Dim ListOfResults = New List(Of TeacherMultiResults)

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

                    Dim AddResult = New TeacherMultiResults With {.Username = "hi", .TestNumber = "1", .TestResult = "2"} 'set up a record
                    ListOfResults.add(AddResult) 'add record to list
                Next
                Information.Close() 'Close read
                Dim bindList = New BindingList(Of TeacherMultiResults)(ListOfResults) 'setup list to bind
                Dim bindsrc = New BindingSource 'setup binding source
                bindsrc.DataSource = bindList 'bind list to source
                DGV_MultiResults.DataSource = bindsrc 'add list to DGV
            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
End Class

edit: Here is my final code/ solution

Imports System.IO
Imports System.ComponentModel
Imports Excel = Microsoft.Office.Interop.Excel

Public Class TeacherMultiResults

Dim LineCount As Integer = 0 'for lines in file
Dim ReadUsername As String = "" 'for file name
Dim ReadTestNumber As Integer = 0 'for reading info
Dim ReadTestResult As Integer = 0 'for reading info
Dim ListOfResults = New List(Of TestResult) 'for loading to DGV

Dim ExcelApp As Excel.Application 'for excel export
Dim ExcelWorkBk As Excel.Workbook 'for excel export
Dim ExcelWorkSht As Excel.Worksheet 'for excel export

Public Class TestResult
    Property Username As String
    Property TestNumber As Integer
    Property TestResult As Integer
End Class

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
        ReadUsername = Files.ToString
        ReadUsername = (ReadUsername.Substring(0, ReadUsername.Length - 4)) 'removes the .txt from the name

        Try
            LineCount = File.ReadAllLines(LoadForm.CurrentDirectory & "\UserResults\" & ReadUsername & ".txt").Length 'amount of lines in file
            If LineCount > 1 Then
                Dim Information As New System.IO.StreamReader(LoadForm.CurrentDirectory & "\UserResults\" & ReadUsername & ".txt") 'opens file

                For i = 0 To LineCount - 2
                    ReadTestNumber = Information.ReadLine() 'reads line to variable
                    ReadTestResult = Information.ReadLine() 'reads line to variable
                    i = i + 1 'adds one to i

                    Dim AddResult = New TestResult With {.Username = ReadUsername, .TestNumber = ReadTestNumber, .TestResult = ReadTestResult} 'set up a record
                    ListOfResults.add(AddResult) 'add record to list
                Next
                Information.Close() 'Close read
                Dim bindList = New BindingList(Of TestResult)(ListOfResults) 'setup list to bind
                Dim bindsrc = New BindingSource 'setup binding source
                bindsrc.DataSource = bindList 'bind list to source
                DGV_MultiResults.DataSource = bindsrc 'add list to DGV
            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
End Class

回答1:


Right, here is what you have to do. I understand that you have manually added your columns since they do not have the same heading as your class properties. This is where the problem lies. Just because you have added a class with three properties, it doesn't mean that your three properties will fill theese columns. The result is therefore the correct amount of rows with empty boxes.

Luckily there is an easy fix for this. Right click your datagridview and select edit columns. Click a column in the list and find the property DataPropertyName in the Bound Column Properties list to the right.

Set Username DataPropertyName to "Username"
Set Test Number DataPropertyName to "TestNumber"
Set Result DataPropertyName to "TestResult"

And then you should be fine.

Oh Now I see what you are doing. Right. Create this class

Public Class TestResult
   Property Username as String
   Property TestNumber as Integer
   Property TestResult as Integer
End Class

Setup the list like this:

Dim ListOfResults = New List(Of TestResult)

Add stuff to the list like this:

Dim AddResult = New TestResult With {.Username = "hi", .TestNumber = "1", .TestResult = "2"}
ListOfResults.add(AddResult) 

Finally setup the bindinglist (Not sure if this step is necessary, but this is the way you built it so I'm just adapting your code to the new solution)

Dim bindList = New BindingList(Of TestResult)(ListOfResults) 

The current reason that the fields are blank is because UserName,TestNumber and TestResult aren't setup as properties. They are public variables, which doesn't cut it. However, doing that as a fix is just plain wrong.



来源:https://stackoverflow.com/questions/22894821/adding-records-to-data-grid-view-from-text-files-no-text-adding-to-dgv-with-lis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!