簡體   English   中英

部分.txt文件到datagridview vb.net

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

我有一個用戶界面表單,可讓您將文本文件上傳到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行中的datagridview中。是否可以這樣做? 就像創建querytabel並選擇固定值一樣?

pe,在第5行中,總是有文本“ Values:”。 之后是否可以選擇所有行以放入datagridview? 我到處都用谷歌搜索,但是什么也沒找到。 而且沒有“示例”代碼給我一個開始。 謝謝你們 !

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