C# DataGridView Excel - Import Data

| Page Views: 1667

We want to see how to import Data from an Excel file to a DataGridView.Thats the aim.

Its easy because Excel and dataGridVeiw both are tabular,hence we can simply map their columns and rows.In fact its even easier as we can use OleDB class to set the Excel as the data source of our DataGridView.

Source Code

Below is the only class we need.  

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CS_DGView_Excel
  public partial class Form1 : Form
    String conString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='D:\Excel\players.xls';Extended Properties=Excel 8.0;";
    OleDbConnection con;
    OleDbDataAdapter adapter;

    public Form1()

    private void loadBtn_Click(object sender, EventArgs e)

    private void clearBtn_Click(object sender, EventArgs e)
      dataGridView1.DataSource = new DataTable();

    private void load()
      con = new OleDbConnection(conString);

      String sql = "SELECT * FROM [Sheet0$]";

      adapter = new OleDbDataAdapter(sql,con);

      DataSet ds=new DataSet();


      dataGridView1.DataSource = ds.Tables[0];

      //CLOSE CON


We have seen how to set the Excel as our data source for our datagridview.We have used OleDBdataAdapter passing in our query statement as well the OleDBConnection object.Of course you shall need to specify the correct path for the Excel file in your computer.


How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:


What do You Think

Previous Post Next Post