38167

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

Question:

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.

<a href="http://gyazo.com/5f314d1ca374abf9f813914609dd931d" rel="nofollow">http://gyazo.com/5f314d1ca374abf9f813914609dd931d</a> (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. <a href="http://gyazo.com/e08f9f2f4ab3971695feffe60503e8a9" rel="nofollow">http://gyazo.com/e08f9f2f4ab3971695feffe60503e8a9</a> (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

Answer1:

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.

Recommend

  • Count line that contain word “118-bonsplans.com” in csv file using php
  • Arrays are null. Not getting information from while loop
  • HTML javascript function issue. [object HTMLInputElement] error output
  • Preferred way to test EditText input in an Android JUnit class
  • a column that increments for every row where column A = number and resets to zero when A = another n
  • WPF TextBox ScrollToLine not updating if visible
  • SQL: Is results order mapped to values in 'IN' expression?
  • Execute Test using updated .feature file without compilation
  • Count wrapped lines in a textbox
  • Nested Random effect in JAGS/ WinBUGS
  • Could not start Selenium session: Failed to start new browser session: Error while launching browser
  • .Net Compact Framework 3.5 HTTP POST
  • Execute SQL Server stored procedure from VBA and retrieve all messages and result sets
  • Create an object whose type is in a string parameter
  • Tensorflow not predicting accurate enough results
  • Format DateAsOrdinal xAxis labels in ZedGraph
  • Copying and renaming unopened workbook in excel
  • Unselect column after pasting data
  • Convert DotNetZip ZipFile to byte array
  • VB.NET Iterating through objects of a structure
  • Alter Table doesn't work under MS Access 64 bit. Why?
  • Sorting a DataTable using LINQ, when sort-by-columns may vary
  • Passing Data between VB.NET forms
  • Pasting URLs That Have Been Scraped From a Webpage
  • how to know the last row filled in vba (excel)? [duplicate]
  • Align Excel cell to center VB - xlCenter is not declared
  • Double-click autofill - dynamic based on adjacent cell
  • Excel VBA URLDownloadToFile Error for https Ressource
  • Creating a Multidimensional, Associative Array in VBScript
  • Need code translation from VB to C#
  • Z3: Convert between FP and BitVector?
  • Conversion from string “a” to type 'Boolean' is not valid
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • Counter field in MS Access, how to generate?
  • How to set my toolbar fixed while scrolling android
  • Convert array of 8 bytes to signed long in C++
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++