2016-02-11 3 views
0

У меня есть форма пользовательского интерфейса, который позволяет вам загрузить текстовый файл в DataGridView следующегочастичного .txt файла DataGridView vb.net

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 в это datagridview, только из строки N. Возможно ли это сделать? Как создание метки запроса и выбор фиксированного значения?

p.e., в строке 5 всегда присутствует текст «Ценности:». Могу ли я выбрать все строки после этого, чтобы поместить в datagridview? я googled везде, но ничего не нашел. и нет «образца» кода, чтобы начать. Спасибо вам всем !

ответ

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 
Смежные вопросы