Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Currently I can read a text file using binaryreader reading the file to various fields on a form and then a section of the file to a datagridview. However, I have 2 datagridviews on the form and I want to be able to separate the text file at a particular string so that it reads into the 2 different datagridviews.

Here is a sample of the text file below. I would like the code to be able to read into Datagridview1 up until it reads the string = Measuring / object data. Starting with that string I would like it to read into Datagridview2.


0200400872;0198800872;0197500908;0194800955;0193000946;0190000911;0187300860;0184000848;0181000842;0177700829;0175100842;0172500848;0169800841;0167200805;0165000785;0163400768;0161800749;0160600724;0158900731;0157800766;0156100774;0155400766;0154500775;0155100782;0154800766;0154500740;0155600727;0157200712;0159500738;0161800738;0164500735;0166900745;0170000763;0173000782;0175700775;0176600744;0177400724;0177300699;01771Measuring / object data;;Project          ; ;Msmt. no.        ; 1;ID number        ; TEST1;%Measuring type   ; pressure strength;No. of msmts.    ; 17;Date             ; 21.03.2013;Time             ; 01;28Interval         ; 10 mm;Strength         ; 37.4 MPa;Breaking stretch ; 00.91 mm;Diameter         ; 0.0 cm;Level            ; 0.0 cm;Direction        ; ;Object species   ; ;Location         ; ;;;Assessment;;From 00.0 cm to 00.0 cm; ;From 00.0 cm to 00.0 cm; ;From 00.0 cm to 00.0 cm; ;From 00.0 cm to 00.0 cm; ;From 00.0 cm to 00.0 cm; ;From 00.0 cm to 00.0 cm; ;;;	Comment;;;;;;;;;;;;;;;;Measuring data;;WDrilling core length [mm] ; Strength [MPa] Breaking angle [deg] ; Breaking stretch [mm];;010.00 ; 43.0 ; 00.95020.00 ; 54.2 ; 01.12030.00 ; 34.2 ; 00.97040.00 ; 28.0 ; 00.78050.00 ; 23.1 ; 00.57060.00 ; 27.1 ; 00.71070.00 ; 20.0 ; 00.51080.00 ; 30.8 ; 00.74090.00 ; 39.4 ; 01.03100.00 ; 46.6 ; 01.04110.00 ; 46.6 ; 01.10120.00 ; 37.9 ; 01.01130.00 ; 23.8 ; 00.83140.00 ; 33.0 ; 01.04150.00 ; 38.0 ; 01.01160.00 ; 52.8 ; 01.06170.00
Posted
Comments
[no name] 14-Jul-13 17:59pm    
Okay so what is the question and/or the problem?
flameback 14-Jul-13 18:08pm    
I cannot separate it at the ReadString "Measuring / object data". It continues to write it all to Datagridview1.

How do I get it to read into Datagridview1 up until it finds "Measuring / object data" and then read the remaining text into Datagridview2?
[no name] 14-Jul-13 19:00pm    
Well what I would do is to read the strings until I get "Measuring / object data" and then switch everything over to Datagridview2.

What you should do to do this is kind of hard to say since you have not told us what you have done or explained why you are unable to find a string.
flameback 14-Jul-13 19:30pm    
This is the code I use to read all text file to datagridview1. Works just as I want it to.
Try
Do
Dim TextLine As String = ""
Dim SplitLine() As String
TextLine = br.ReadString
SplitLine = Split(TextLine, ";")
.DataGridView1.Rows.Add(SplitLine)
Loop

Catch ex As Exception

End Try

However, this does not seem to work:

Try
Do
Dim TextLine As String = ""
Dim SplitLine() As String
TextLine = br.ReadString
SplitLine = Split(TextLine, ";")
.DataGridView1.Rows.Add(SplitLine)
Loop Until br.ReadString = "Measuring / object data"

Catch ex As Exception

End Try

Nor does Do Until br.ReadString = "Measuring / object data"
Essential I want it to stop reading at that ReadLine and start reading the remaining text to Datagridview2
[no name] 14-Jul-13 19:59pm    
If you ran you code in the debugger, I think that you would find that the line that you want to switch on is being skipped because you are reading the string as a condition of your loop and you are reading the string inside your loop. I would try changing my loop condition to seeing if TextLine Contains the text you want.

The first thing to do is to read the whole file into a string.
You can then use String.Split to break it two parts: before the "Measuring / object data" and after.
Then, use String.Split to break it into lines, then break those into individual elements to add ad rows.

Without an actual copy of your file it's difficult to tell, but it's along the lines of:
VB
Dim rawFile As String = File.ReadAllText(path)
Dim halves As String() = rawFile.Split("Measuring / object data")
Dim lines As String() = halves(0).Split(vbLf)
For Each line As String In lines
	Dim columns As String = line.Split(";")
	DataGridView1.Rows.Add(columns)
Next
lines = halves(1).Split(vbLf)
For Each line As String In lines
	Dim columns As String = line.Split(";")
	DataGridView1.Rows.Add(columns)
Next
 
Share this answer
 
Comments
flameback 16-Jul-13 17:19pm    
Griff, thanks for your solution attempt. However it does not seem to work. Further information might help. This is output text uploaded from a testing device that writes the data with binarywriter in text format but with a different file extension (*.wcf). For the most part individual recordings are separated by semicolons, but we need to split the data at "Measuring /object data" between 2 different DGV's.
OriginalGriff 17-Jul-13 3:55am    
"it does not seem to work" is not very helpful! :laugh:
Would you care to elaborate on what the problem is?
Currently this is my temporary solution that works so that I can continue with the project for now. However if anyone can suggest a better solution please do so.
Please note: I know that this is a nasty way to solve the problem but I need to continue for now. Reply comments that point out the obvious just seem pointless.


VB
Try
                            For i As Integer = 0 To 70
                                .DataGridView1.Rows.Add(br.ReadString.Split(";"))
                            Next i

                            Do
                                .DataGridView2.Rows.Add(br.ReadString.Split(";"))
                            Loop

                        Catch ex As Exception

                        End Try
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900