Adding records to Data Grid View from text files. No text adding to DGV with lists VB.net -
i'm looking issue having. have multiple text files in folder. folder can have "unlimited" amount of text files in it, although typically 2-150 files.
http://gyazo.com/5f314d1ca374abf9f813914609dd931d (images + below, can't embed due lack of reputation)
each file contains "unlimited" (although typically 0-20 lines) amount of data inside it. line 1 being "test number" , line 2 being "test result" - seen in image above
my data grid view has 3 columns in [username, test number , test result] named (column_username, column_testnumber, column_testresult) - seen in image above
when running current code have, there correct amount of grids in data grid view, although of cells empty. http://gyazo.com/e08f9f2f4ab3971695feffe60503e8a9 (image of this)
one thing don't want change of text file/ folder structures, though i'm aware inefficient storage. although i'm more happy use instead of lists function if think there better way.
how can fix this? i'm using vb.net (visual studios 2013)
if need more information, please ask.
many thanks.
imports system.io imports system.componentmodel public class teachermultiresults dim linecount integer = 0 dim username string = "" dim testnumber integer = 0 dim testresult integer = 0 dim listofresults = new list(of teachermultiresults) private sub teachermultiresults_load(sender object, e eventargs) handles mybase.load dim directory new io.directoryinfo(loadform.currentdirectory & "\userresults\") 'selects directory dim filenames io.fileinfo() = directory.getfiles() dim files io.fileinfo each files in filenames 'list names of files in specified directory username = files.tostring username = (username.substring(0, username.length - 4)) 'removes .txt name try linecount = file.readalllines(loadform.currentdirectory & "\userresults\" & username & ".txt").length 'amount of lines in file if linecount > 1 dim information new system.io.streamreader(loadform.currentdirectory & "\userresults\" & username & ".txt") 'opens file linecount = linecount / 2 'halfs line count = 0 linecount - 1 testnumber = information.readline() 'reads line variable testresult = information.readline() 'reads line variable = + 1 'adds 1 dim addresult = new teachermultiresults {.username = "hi", .testnumber = "1", .testresult = "2"} 'set record listofresults.add(addresult) 'add record list next information.close() 'close read dim bindlist = new bindinglist(of teachermultiresults)(listofresults) 'setup list bind dim bindsrc = new bindingsource 'setup binding source bindsrc.datasource = bindlist 'bind list source dgv_multiresults.datasource = bindsrc 'add list dgv end if catch ex 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 final code/ solution
imports system.io imports system.componentmodel imports excel = microsoft.office.interop.excel public class teachermultiresults dim linecount integer = 0 'for lines in file dim readusername string = "" 'for file name dim readtestnumber integer = 0 'for reading info dim readtestresult integer = 0 'for reading info dim listofresults = new list(of testresult) 'for loading dgv dim excelapp excel.application 'for excel export dim excelworkbk excel.workbook 'for excel export dim excelworksht excel.worksheet 'for excel export public class testresult property username string property testnumber integer property testresult integer end class private sub teachermultiresults_load(sender object, e eventargs) handles mybase.load dim directory new io.directoryinfo(loadform.currentdirectory & "\userresults\") 'selects directory dim filenames io.fileinfo() = directory.getfiles() dim files io.fileinfo each files in filenames 'list names of files in specified directory readusername = files.tostring readusername = (readusername.substring(0, readusername.length - 4)) 'removes .txt name try linecount = file.readalllines(loadform.currentdirectory & "\userresults\" & readusername & ".txt").length 'amount of lines in file if linecount > 1 dim information new system.io.streamreader(loadform.currentdirectory & "\userresults\" & readusername & ".txt") 'opens file = 0 linecount - 2 readtestnumber = information.readline() 'reads line variable readtestresult = information.readline() 'reads line variable = + 1 'adds 1 dim addresult = new testresult {.username = readusername, .testnumber = readtestnumber, .testresult = readtestresult} 'set record listofresults.add(addresult) 'add record list next information.close() 'close read dim bindlist = new bindinglist(of testresult)(listofresults) 'setup list bind dim bindsrc = new bindingsource 'setup binding source bindsrc.datasource = bindlist 'bind list source dgv_multiresults.datasource = bindsrc 'add list dgv end if catch ex 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
right, here have do. understand have manually added columns since not have same heading class properties. problem lies. because have added class 3 properties, doesn't mean 3 properties fill theese columns. result therefore correct amount of rows empty boxes.
luckily there easy fix this. right click datagridview , select edit columns. click column in list , find property datapropertyname in bound column properties list right.
set username datapropertyname "username" set test number datapropertyname "testnumber" set result datapropertyname "testresult"
and should fine.
oh see doing. right. create class
public class testresult property username string property testnumber integer property testresult integer end class
setup list this:
dim listofresults = new list(of testresult)
add stuff list this:
dim addresult = new testresult {.username = "hi", .testnumber = "1", .testresult = "2"} listofresults.add(addresult)
finally setup bindinglist (not sure if step necessary, way built i'm adapting code new solution)
dim bindlist = new bindinglist(of testresult)(listofresults)
the current reason fields blank because username,testnumber , testresult aren't setup properties. public variables, doesn't cut it. however, doing fix plain wrong.
Comments
Post a Comment