部分.txt文件到datagridview vb.net

时间:2016-02-11 16:18:19

标签: vb.net datagridview

我有一个用户界面表单,您可以将文本文件上传到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

好吧,现在我的问题是我不想将完整的txt文件放在数据网格视图中,只是从第N行开始。 有可能吗? 喜欢创建查询标签并选择固定值吗?

p.e。,在第5行,总是文本"值:" 。我可以选择之后的所有行来放入datagridview吗?我到处搜索,但一无所获。并且没有"样本"代码给我一个开始。谢谢大家!

1 个答案:

答案 0 :(得分:0)

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