如何将.csv文件导入到vb.net中的datagridview?

时间:2017-05-15 08:01:33

标签: vb.net datagridview csv-import

我用过的代码是:

java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{213d192 position=3 id=-1, oldPos=1, pLpos:1 scrap [attachedScrap] tmpDetached no parent}
                                                                       at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5297)
                                                                       at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5479)
                                                                       at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
                                                                       at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
                                                                       at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
                                                                       at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)
                                                                       at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
                                                                       at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
                                                                       at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3534)
                                                                       at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3310)
                                                                       at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3844)
                                                                       at android.view.View.layout(View.java:16653)
                                                                       at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                       at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:636)
                                                                       at android.view.View.layout(View.java:16653)
                                                                       at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                       at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1080)
                                                                       at android.view.View.layout(View.java:16653)
                                                                       at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                       at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                       at android.view.View.layout(View.java:16653)
                                                                       at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                       at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
                                                                       at android.view.View.layout(View.java:16653)
                                                                       at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                       at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                       at android.view.View.layout(View.java:16653)
                                                                       at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                       at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
                                                                       at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
                                                                       at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
                                                                       at android.view.View.layout(View.java:16653)
                                                                       at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                       at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                       at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
                                                                       at android.view.View.layout(View.java:16653)
                                                                       at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                       at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2198)
                                                                       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1958)
                                                                       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1134)
                                                                       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6050)
                                                                       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:860)
                                                                       at android.view.Choreographer.doCallbacks(Choreographer.java:672)
                                                                       at android.view.Choreographer.doFrame(Choreographer.java:608)
                                                                       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:846)
                                                                       at android.os.Handler.handleCallback(Handler.java:739)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                       at android.os.Looper.loop(Looper.java:148)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5441)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)

我的输出看起来错误编码: enter image description here

我的代码出了什么问题?请帮我。谢谢你的考虑。

2 个答案:

答案 0 :(得分:0)

我是从Google到达这里的。以防万一有人在寻找快速代码来复制:

    Private Sub ReadCSV()
        Dim fName As String = "C:\myfile.csv"
        Dim TextLine As String = ""
        Dim SplitLine() As String

        If System.IO.File.Exists(fName) = True Then
            Using objReader As New System.IO.StreamReader(fName, Encoding.ASCII)
                Do While objReader.Peek() <> -1
                    TextLine = objReader.ReadLine()
                    SplitLine = Split(TextLine, ";")
                    Me.DataGridView1.Rows.Add(SplitLine)
                Loop
            End Using
        Else
            MsgBox("File Does Not Exist")
        End If
    End Sub

答案 1 :(得分:-1)

这真的很有用!

Dim fName As String = ""
   OpenFileDialog1.InitialDirectory = "c:\desktop"
       OpenFileDialog1.Filter = "CSV files(*.csv)|*.csv"  
    OpenFileDialog1.FilterIndex = 2
    OpenFileDialog1.RestoreDirectory = True
    If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
        fName = OpenFileDialog1.FileName
    End If
    txtpathfile.Text = fName
    Dim TextLine As String = ""
    Dim SplitLine() As String


    If System.IO.File.Exists(fName) = True Then
        Dim objReader As New System.IO.StreamReader(txtpathfile.Text, Encoding.ASCII)
        Do While objReader.Peek() <> -1
            TextLine = objReader.ReadLine()
            SplitLine = Split(TextLine, ";")
            Me.DataGridView1.Rows.Add(SplitLine)
        Loop
    Else
        MsgBox("File Does Not Exist")
    End If