0 Replies - 774 Views - Last Post: 06 April 2014 - 07:32 AM Rate Topic: -----

#1 theovencook   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-April 14

Adding records to Data Grid View from text files. Using lists

Posted 06 April 2014 - 07:32 AM

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/5f3...813914609dd931d (images for this + below)

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/e08...5feffe60503e8a9 (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

                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
                    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

Is This A Good Question/Topic? 0
  • +

Page 1 of 1