SiteMap  | About    

How to Import data from Excel to DataGridView

The DataGridView control and its related classes are designed to be a flexible, extensible system for displaying and editing tabular data. You can use a DataGridView control to display data with or without an underlying data source.

The following source code shows how to Import data from an Excel file to a DataGridView control .

Imports System.Data.SqlClient
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim MyConnection As System.Data.OleDb.OleDbConnection
        Dim DtSet As System.Data.DataSet
        Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
        MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\';Extended Properties=Excel 8.0;")
        MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
        MyCommand.TableMappings.Add("Table", "")
        DtSet = New System.Data.DataSet
        DataGridView1.DataSource = DtSet.Tables(0)

    End Sub
End Class (C) 2016    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.