Error vb.net import excel to DataGridView [closed]

1

I am doing an import of data from an Excel spreadsheet to a datagrid on VB.net

Something I managed to do, if I assign the DataTable to the DataGrid. But what I really need is to move field to field from the spreadsheet to the DataGrid.

To do this I created a function called load_sheet, and it is like that.

Private Sub cargar_hoja(f As String, s As String)
    Dim dz As New DataTable
    Dim cont As Integer = 1

    If ((String.IsNullOrEmpty(f)) OrElse (String.IsNullOrEmpty(s))) Then _
        Throw New ArgumentNullException()
    Try
        Dim sql As String = "SELECT * FROM [" & s & "$]"
        Dim adaptador As New OleDbDataAdapter(sql, Conexion)
        Dim ds As New DataTable("Excel")
        Dim dt As New DataTable
        adaptador.Fill(ds)

        dt = limpiarFilas(ds)

        gvRegistros.DataSource = dt

    Catch ex As Exception
        MsgBox("Error: " + ex.Message, MsgBoxStyle.Exclamation)
    End Try
End Sub

This works, the problem is when I try to pass parameter to parameter for the datagrid, and I try to do it in the following way.

Private Sub cargar_hoja(f As String, s As String)
    Dim dz As New DataTable
    Dim cont As Integer = 1

    If ((String.IsNullOrEmpty(f)) OrElse (String.IsNullOrEmpty(s))) Then _
        Throw New ArgumentNullException()
    Try
        Dim sql As String = "SELECT * FROM [" & s & "$]"
        Dim adaptador As New OleDbDataAdapter(sql, Conexion)
        Dim ds As New DataTable("Excel")
        Dim dt As New DataTable
        adaptador.Fill(ds)

        dt = limpiarFilas(ds)

        While dt.Rows.Count > cont
            gvRegistros.Rows.Add(1)

            gvRegistros.Item("col1", cont).Value() = dz.Rows(cont)("B")
            gvRegistros.Item("col2", cont).Value() = dz.Rows(cont)("C")

            cont += 1
        End While

    Catch ex As Exception
        MsgBox("Error: " + ex.Message, MsgBoxStyle.Exclamation)
    End Try
End Sub

The Error it throws is: "There is no row in position 1"

I hope you can help me

    
asked by Luippo 25.10.2017 в 16:07
source

0 answers