简体   繁体   English

CSV文件到使用列表的datagridview

[英]CSV file to datagridview using list

This is similar to: https://social.msdn.microsoft.com/Forums/en-US/7b0e28b4-c1ef-49d6-8f46-11b379428052/import-from-csv-file-to-two-dimensional-array?forum=vbgeneral 这类似于: https : //social.msdn.microsoft.com/Forums/en-US/7b0e28b4-c1ef-49d6-8f46-11b379428052/import-from-csv-file-to-二维数组?论坛= vbgeneral

but the code that Cor Ligthert suggests doesn't work even though its exactly what I need. 但是Cor Ligthert建议的代码即使确实正是我所需要的也不起作用。

The code is: 代码是:

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim FILE_NAME As String = "C:\Users\Admin\Documents\test.csv"

    Dim OutlookLines As New List(Of OutlookLine)
    Dim sr As New IO.StreamReader(FILE_NAME)
    Do Until sr.EndOfStream
        OutlookLines.Add(New OutlookLine With {.line = sr.ReadLine})
    Loop
    DataGridView1.DataSource = OutlookLines

End Sub


Private Class OutlookLine
    Private theLine As String
    Public Property line() As String
        Get
            Return theLine
        End Get
        Set(ByVal value As String)
            theLine = value
        End Set
    End Property
End Class

End Class

I try putting in: 我尝试输入:

.Split(","c)

after sr.ReadLine but it says "Value of type 1-D array of String cannot be converted to String" 在sr.ReadLine之后,但显示“无法将String的类型1-D数组的值转换为String”

The code above works fine but each row of the csv is scrunched into one column (obviously because I am not splitting it at the ","). 上面的代码工作正常,但csv的每一行都被压缩为一列(显然是因为我没有将其拆分为“,”)。

csv data: CSV数据:

1,2,3,4,5
6,7,8,9,0

See if this works for you: 看看这是否适合您:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    DataGridView1.DataSource = CSVToDataTable("C:\Users\Admin\Documents\test.csv", True)
End Sub

Private Function CSVToDataTable(filePath As String, Optional hasHeaderRow As Boolean = False) As DataTable
    Dim rows = IO.File.ReadAllLines(filePath).Select(Function(l) l.Split(","c))

    Dim dt = New DataTable
    Dim count = 0

    dt.Columns.AddRange(rows.First.Select(Function(c) New DataColumn With {.ColumnName = If(hasHeaderRow, c, "Column" & Threading.Interlocked.Increment(count))}).ToArray())

    For Each row In rows.Skip(If(hasHeaderRow, 1, 0))
        Dim dr = dt.NewRow()
        dr.ItemArray = row
        dt.Rows.Add(dr)
    Next

    Return dt
End Function

If you wish for the line property of the OutlookLine class to be an array of string, you should define it like this: 如果希望OutlookLine类的line属性是字符串数组,则应按以下方式定义它:

Private Class OutlookLine
    Private theLine As String()
    Public Property line As String()
        Get
            Return theLine
        End Get
        Set(ByVal value As String())
            theLine = value
        End Set
    End Property
End Class

Then this will work: 然后这将起作用:

        OutlookLines.Add(New OutlookLine With {.line = sr.ReadLine.Split(",")})

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

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