读取文本文件并将数据分成2个不同的datagridviews [英] Read text file and seperate data into 2 different datagridviews

查看:71
本文介绍了读取文本文件并将数据分成2个不同的datagridviews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我可以使用二进制读取器读取文本文件,将文件读取到表单上的各个字段,然后将文件的一部分读取到datagridview。但是,我在表单上有2个datagridviews,我希望能够将文本文件分成特定的字符串,以便它读入2个不同的数据网格。



以下是文本文件的示例。我希望代码能够读取Datagridview1,直到它读取string = Measuring / object数据。从该字符串开始,我希望它读入Datagridview2。





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

推荐答案

要做的第一件事就是将整个文件读成一个字符串。

然后你可以使用String.Split将它分成两部分:在测量/对象数据之前和之后。

然后,使用String.Split将其分成行,然后将它们分解为单个元素添加广告行。



如果没有文件的实际副本,很难说清楚,但它符合以下几行:

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


目前这是我的临时解决方案,可以让我现在继续这个项目。但是,如果有人能提出更好的解决方案,请这样做。

请注意:我知道这是解决问题的一种令人讨厌的方法,但我现在需要继续。回复评论指出明显的看似毫无意义。





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.


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


这篇关于读取文本文件并将数据分成2个不同的datagridviews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆