How to Import data from Excel to DataGridView

The DataGridView control, along with its associated classes, has been meticulously designed to provide a highly adaptable and scalable framework for effortlessly showcasing and modifying tabular data. It serves as a versatile solution for presenting data in a structured table format.

With the DataGridView control, you have the option to display data in a variety of ways. It offers the capability to exhibit information either by utilizing an underlying data source or by manually populating the control with data. This means that you can seamlessly showcase data in the DataGridView control, whether it is sourced from a database, an external file, or generated programmatically within your application.

Import data from an Excel file to a DataGridView control

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

Full Source VB.NET
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 MyCommand.Fill(DtSet) DataGridView1.DataSource = DtSet.Tables(0) MyConnection.Close() End Sub End Class