.CSV到DataGridView行在VB.NET中变为空白 [英] .CSV to DataGridView Rows coming up blank in VB.NET
问题描述
这是我要导入到VB.NET应用程序中的CSV数据:
Here is the CSV data I am trying to import into my VB.NET app:
但是当我通过应用程序运行它时,它只会填充最后一行:
But when I run it through the app it only populates the last row:
这是导入的代码:
Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
DataGridView1.ClearSelection()
ofd.Filter = "(*csv)|*.csv"
If ComboBox1.Text = "zVBImportTEST" Then
If (ofd.ShowDialog() = DialogResult.OK) Then
txtbxFilePath.Text = ofd.FileName
End If
Dim colsexpected As Integer = 6
Dim thereader As New StreamReader(txtbxFilePath.Text, Encoding.ASCII)
Dim sline As String = ""
thereader.ReadLine()
Do
sline = thereader.ReadLine
If sline Is Nothing Then Exit Do
Dim words() As String = sline.Split(",")
DataGridView1.Rows.Add("")
For ix As Integer = 0 To 5
DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(ix).Value = words(ix)
Next
Loop
thereader.Close()
Else
MessageBox.Show("Please select a project.", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
End If
End Sub
我似乎无法弄清楚为什么其他行变成空白. 如果有人可以帮助我,将不胜感激.
I can't seem to figure out why the other rows are coming up blank. If anyone could help me it would be greatly appreciated.
推荐答案
我不建议尝试通过StreamReader读取数据,而是在传递CSV文件的连接字符串时使用OleDb类.
I would not suggest trying to read the data via a StreamReader, instead use the OleDb class while passing the connection string for a CSV file.
这是一个基于CSV文件内容返回DataTable的函数的快速示例:
Here is a quick example of a function that returns a DataTable based on the contents of the CSV file:
Private Function ConvertCSVToDataTable(ByVal path As String) As DataTable
Dim dt As DataTable = New DataTable()
Using con As OleDb.OleDbConnection = New OleDb.OleDbConnection()
Try
con.ConnectionString = String.Format("Provider={0};Data Source={1};Extended Properties=""Text;HDR=YES;FMT=Delimited""", "Microsoft.Jet.OLEDB.4.0", IO.Path.GetDirectoryName(path))
Using cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("SELECT * FROM " & IO.Path.GetFileName(path), con)
Using da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(cmd)
con.Open()
da.Fill(dt)
con.Close()
End Using
End Using
Catch ex As Exception
Console.WriteLine(ex.ToString())
Finally
If con IsNot Nothing AndAlso con.State = ConnectionState.Open Then
con.Close()
End If
End Try
End Using
Return dt
End Function
然后这是您绑定DataGridView的方式:
Then here is how you'd bind your DataGridView:
DataGridView1.DataSource = Me.ConvertCSVToDataTable(ofd.FileName)
更新
由于您要指定DataColumn的数据类型,因此,声明一个DataTable并将其分配给自定义函数,然后再进行修改并更改特定列的数据类型.这是一个快速的(自由类型且未经测试的)示例:
Since you want to specify the data type of the DataColumn, declare a DataTable and assign it to the custom function, but then go in after the fact and change the data type of the specific columns. Here is a quick (free-typed and untested) example:
Dim csv As DataTable = Me.ConvertCSVToDataTable(ofd.FileName)
With csv.Columns
.Items(0).DataType = GetType(Int32)
.Items(1).DataType = GetType(Int32)
.Items(4).DataType = GetType(Int32)
.Items(5).DataType = GetType(Int32)
End With
DataGridView1.DataSource = csv
这篇关于.CSV到DataGridView行在VB.NET中变为空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!