![](/img/trans.png)
[英]Host custom controls on DataGridView which populate data from DataTable
[英]Trying Populate DataGridView or DataTable from a CSV, which is an embedded resource
好吧,已經有一段時間嘗試這樣做了,並且一直在大量站點中尋找線索,但是沒有運氣。
我正在嘗試使用添加為資源的逗號分隔的CSV填充DataTable或DataGridView(將其拖放到vb.net-2013資源窗口上)。
我已經設法使DataGridView列顯示CSV的第一行,但其余部分不行。 CSV看起來像:
"id,city,loc,lat,lon,chk,tst,sob,completed,1,charlotte,dock,0000.0,0000.0,,,,,,2,Chicago,..."
每當我嘗試填充行時,都會出現錯誤:
Input array is longer than the number of columns in this table.
有9列,約70萬行,第一行是列名。
謝謝你的幫助。
這是我到目前為止的內容:
Dim tbl = New DataTable
Dim lines() As String = My.Resources.CityLocation.Split(CChar(Environment.NewLine))
Dim colCount = lines.First.Split(","c).Length
Dim col = Split(lines(0), ","c)
Dim rowCount = lines.GetLength(0)
'Populating the Column Headers
For i = 0 To colCount - 1
tbl.Columns.Add(New DataColumn(col(i), GetType(String)))
Next
''Populating the Rows
'For i = 1 To rowCount
' For Each line In lines
' Dim theRow = From field In line.Split(","c)
' Dim Array() = theRow.ToArray
' Dim newRow = tbl.Rows.Add()
' newRow.ItemArray = Array
' Next
'Next
`
我建議您使用TextFieldParser
。 如果您將文本文件嵌入為資源,則它將作為String
返回。 您可以在其上創建一個StringReader
。 StringReader
類似於StreamReader
但用於讀取String
而不是Stream
。 然后,您可以在其上創建一個TextFieldParser
。
Using reader As New StringReader(My.Resources.CityLocation),
parser As New TextFieldParser(reader)
'...
End Using
如果您查看TextFieldParser
類的MSDN文檔,則將找到一個示例,該示例說明了如何使用它來讀取CSV數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.