VB.NET 读取CSV文件到datatable中后,数据被重新排序了,请大神帮忙看看哪里有问题
用下面的函数从CSV文件中读取数据到datatable中,然后再用datagridview显示,结果发现数据到datatable中后,被排序了,请帮忙看看哪里出错了,函数...
用下面的函数从CSV文件中读取数据到datatable中,然后再用datagridview显示,结果发现数据到datatable中后,被排序了,请帮忙看看哪里出错了,函数是在网上找的。
Public Function CSVToDataTable(ByVal FilePath As String) As DataTable
Try
If (System.IO.File.Exists(FilePath)) Then
Dim fi As New System.IO.FileInfo(FilePath)
'HDR=NO 第一行当数据处理
'HDR=YES(默认)第一行当列处理
Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Text;HDR=NO';Data Source=" & fi.DirectoryName
Dim objConn As New System.Data.OleDb.OleDbConnection(sConnectionString)
objConn.Open()
Dim strColum As String
Dim objCmdSelect As New Data.OleDb.OleDbCommand("SELECT Distinct * FROM " & fi.Name, objConn)
Dim objAdapter As New Data.OleDb.OleDbDataAdapter
Dim dt As New DataTable
objAdapter.SelectCommand = objCmdSelect
objAdapter.Fill(dt)
objConn.Close()
'Debug.Print(dt(0))
MsgBox(dt.Rows(0)(0).ToString())
DataGridView1.DataSource = dt
Label1.Text = DataGridView1.Rows.Count - 1
Return dt
End If
Catch ex As Exception
MsgBox(ex.ToString())
Return Nothing
End Try
End Function 展开
Public Function CSVToDataTable(ByVal FilePath As String) As DataTable
Try
If (System.IO.File.Exists(FilePath)) Then
Dim fi As New System.IO.FileInfo(FilePath)
'HDR=NO 第一行当数据处理
'HDR=YES(默认)第一行当列处理
Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Text;HDR=NO';Data Source=" & fi.DirectoryName
Dim objConn As New System.Data.OleDb.OleDbConnection(sConnectionString)
objConn.Open()
Dim strColum As String
Dim objCmdSelect As New Data.OleDb.OleDbCommand("SELECT Distinct * FROM " & fi.Name, objConn)
Dim objAdapter As New Data.OleDb.OleDbDataAdapter
Dim dt As New DataTable
objAdapter.SelectCommand = objCmdSelect
objAdapter.Fill(dt)
objConn.Close()
'Debug.Print(dt(0))
MsgBox(dt.Rows(0)(0).ToString())
DataGridView1.DataSource = dt
Label1.Text = DataGridView1.Rows.Count - 1
Return dt
End If
Catch ex As Exception
MsgBox(ex.ToString())
Return Nothing
End Try
End Function 展开
1个回答
展开全部
首先,在填充后的datatable处设置一个断点,查看datatable中的数据是什么样的,判断是读取出了问题还是绑定到datagridview时候出的问题。然后补充问题告诉我datatable中的数据是什么样的
追问
已查看过了,数据在datatable中已错了,CSV数据格式如下:1行:Vertical Position,-1.000E+01,
2行:-5.000000e+00,13.400000,-5.000000e+00,0.400000,
MsgBox(dt.Rows(0)(0).ToString())查看出来的数据是-1,因此确定是数据进入datatable是已被按数字,字符的顺序进行排序了。
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询