简体   繁体   English

部分.txt文件到datagridview vb.net

[英]partial .txt file to datagridview vb.net

i have a user interface form that let's you upload a text file to a datagridview as follows 我有一个用户界面表单,可让您将文本文件上传到datagridview,如下所示

Sub Datagrid()
    Dim sw = System.Diagnostics.Stopwatch.StartNew()
    Using stream As System.IO.FileStream = System.IO.File.OpenRead(TextBox1.Text)
        Using reader As New System.IO.StreamReader(stream)
            Dim line As String = reader.ReadLine()
            While (line IsNot Nothing)
                Dim columns = line.Split(";")
                line = reader.ReadLine()
                Dim index = Me.DataGridView1.Rows.Add()
                Me.DataGridView1.Rows(index).SetValues(columns)
            End While
        End Using
    End Using
    sw.Stop()
End Sub

Well, now my problem is that i don't want to put the full txt file in that datagridview, just from line N. Is it possible to do that? 好吧,现在我的问题是我不想将完整的txt文件放在N行中的datagridview中。是否可以这样做? Like creating a querytabel and selecting a fixed value? 就像创建querytabel并选择固定值一样?

pe, in line 5 there's always the text "Values:" . pe,在第5行中,总是有文本“ Values:”。 Can i select all the lines after that to put in the datagridview? 之后是否可以选择所有行以放入datagridview? i googled everywhere but found nothing. 我到处都用谷歌搜索,但是什么也没找到。 and there's no "sample" code to give me a start . 而且没有“示例”代码给我一个开始。 thank you all ! 谢谢你们 !

Dim n As Integer = 5
    Dim lines As IEnumerable(Of String) = IO.File.ReadAllLines("textbox1.text").Skip(n) 
'Gets every line after a certain line count

    'Create a new datatable and add some columns
Dim dt As New DataTable
dt.Columns.AddRange((From columnIndex As Integer In Enumerable.Range(1, lines.First.Split(";"c).Count) Select New DataColumn("Column" & columnIndex.ToString())).ToArray())

    'Add each line as a row to the datatable
    For Each line As String In lines
        dt.Rows.Add(line.Split(";"c))
    Next

    'Set the datasource of the datagridview
    MyDataGridView.DataSource = dt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM