We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Excel to DataGrid or DataGridView in C#

Platform: WinForms |
Control: XlsIO |
Published Date: August 6, 2018 |
Last Revised Date: April 9, 2019

Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files. Using this library, you can load data from Excel to DataGrid or DataGridView in C#.

Using XlsIO, data can be loaded from Excel to DataGrid or DataGridView in the following ways:

Steps to load data from Excel to DataGrid or DataGridView, programmatically:

Step 1: Create a new Windows Forms application project.

Create a Windows Forms application in Visual Studio

Create a new Windows Forms application

Step 2: Install the Syncfusion.XlsIO.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.

Add XlsIO reference to the project

Install NuGet package

Step 3: Include the following namespaces in the Program.cs file for DataGrid and XlsIO usage.

C#

using System.Windows.Forms;
using System.Data;
using Syncfusion.XlsIO;

 

VB.NET

Imports System.Windows.Forms 
Imports System.Data
Imports Syncfusion.XlsIO

 

Step 4: Add the DataGrid control in Form1.cs.

C#

//Initialize DataGrid control
DataGrid dataGrid = new DataGrid();

 

VB.NET

'Initialize DataGrid control
Dim dataGrid As DataGrid = New DataGrid()

 

Step 5: Add a new button to load data from Excel file to DataGrid.

C#

private DataGrid dataGrid;
private Button btnImport;
private Label label;
 
private void InitializeComponent()
{
  btnImport = new Button();
  dataGrid = new DataGrid();
  label = new Label();
    
  //Button   
  btnImport.Location = new System.Drawing.Point(41, 404);
  btnImport.Size = new System.Drawing.Size(280, 23);
  btnImport.Text = "Load Excel to DataGrid";
  btnImport.Click += new EventHandler(btnImport_Click);
 
  //Label
  label.Location = new System.Drawing.Point(0, 50);
  label.Size = new System.Drawing.Size(426, 48);
  label.Text = "Click the button to load Excel spreadsheet to DataGrid through Essential XlsIO.";
  label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
 
  //DataGrid
  dataGrid.Location = new System.Drawing.Point(29, 96);
  dataGrid.Size = new System.Drawing.Size(312, 292);
   
  //Excel to DataGrid 
  ClientSize = new System.Drawing.Size(425, 434);
  Controls.Add(dataGrid);
  Controls.Add(label);
  Controls.Add(btnImport);
  Text = "Load DataGrid";
}

 

VB.NET

Private WithEvents label As Label
Private WithEvents btnImport As Button
Private WithEvents dataGrid As DataGrid
 
Private Sub InitializeComponent()
  label = New Label()
  btnImport = New Button()
  dataGrid = New DataGrid()
 
  'Button
  btnImport.Location = New Point(41, 404)
  btnImport.Size = New Size(280, 23)
  btnImport.Text = "Load Excel to DataGrid"
 
  'Label
  label.Location = New Point(0, 50)
  label.Size = New Size(426, 48)
  label.Text = "Click the button to load Excel spreadsheet to DataGrid through Essential XlsIO."
  label.TextAlign = ContentAlignment.MiddleCenter
 
  'DataGrid
  dataGrid.Location = New Point(29, 96)
  dataGrid.Size = New Size(312, 292)
 
  'Excel to DataGrid
  ClientSize = New Size(425, 434)
  Controls.Add(dataGrid)
  Controls.Add(label)
  Controls.Add(btnImport)
  Text = "Load DataGrid"
End Sub

 

Step 6: Use the following code snippet to load the Excel workbook.

C#

//Initialize the Excel Engine
using (ExcelEngine excelEngine = new ExcelEngine())
{
  //Initialize Application
  IApplication application = excelEngine.Excel;
  
  //Set default version for application.
  application.DefaultVersion = ExcelVersion.Excel2013;
  
  //Open existing workbook with data entered
  Assembly assembly = typeof(Form1).GetTypeInfo().Assembly;
  Stream fileStream = assembly.GetManifestResourceStream("ExceltoDataGrid.Sample.xlsx");
  IWorkbook workbook = application.Workbooks.Open(fileStream);
  
  //Accessing first worksheet in the workbook
  IWorksheet worksheet = workbook.Worksheets[0];
  
  //No exception will be thrown if there are unsaved workbooks.
  excelEngine.ThrowNotSavedOnDestroy = false;
}

 

VB.NET

'Initialize the Excel Engine
Using excelEngine As ExcelEngine = New ExcelEngine()
 
  'Initialize Application
  Dim application As IApplication = excelEngine.Excel
 
  'Set default version for application.
  application.DefaultVersion = ExcelVersion.Excel2013
 
  'Open existing workbook with data entered
  Dim assembly As Assembly = GetType(Form1).GetTypeInfo.Assembly
  Dim fileStream As Stream = assembly.GetManifestResourceStream("ExceltoDataGrid.Sample.xlsx")
  Dim workbook As IWorkbook = application.Workbooks.Open(fileStream)
 
  'Accessing first worksheet in the workbook
  Dim worksheet As IWorksheet = workbook.Worksheets(0)
 
  'No exception will be thrown if there are unsaved workbooks.
  excelEngine.ThrowNotSavedOnDestroy = False
 
End Using

 

Step 7: Export the Excel sheet data to a DataTable using ExportDataTable() method.

C#

//Export data from Excel worksheet
DataTable customersTable = worksheet.ExportDataTable(worksheet.UsedRange, ExcelExportDataTableOptions.ColumnNames);

 

VB.NET

'Export data from Excel worksheet
Dim customersTable As DataTable = worksheet.ExportDataTable(worksheet.UsedRange, ExcelExportDataTableOptions.ColumnNames)

 

Step 8: Load the exported DataTable to DataGrid object by setting DataSource property.

C#

//Load the data to dataGrid
dataGrid.DataSource = customersTable;

 

VB.NET

'Load the data to dataGriddataGrid.DataSource = customersTable

 

Step 9: Use the following code to load data from Excel to DataGrid or DataGridView in C# and VB.NET, using XlsIO.

C#

//Initialize the Excel Engine
using (ExcelEngine excelEngine = new ExcelEngine())
{
  //Initialize Application
  IApplication application = excelEngine.Excel;
 
  //Set default version for application.
  application.DefaultVersion = ExcelVersion.Excel2013;
 
  //Open existing workbook with data entered
  Assembly assembly = typeof(Form1).GetTypeInfo().Assembly;
  Stream fileStream = assembly.GetManifestResourceStream("ExceltoDataGrid.Sample.xlsx");
  IWorkbook workbook = application.Workbooks.Open(fileStream);
 
  //Accessing first worksheet in the workbook
  IWorksheet worksheet = workbook.Worksheets[0];
 
  //Export data from Excel worksheet.
  DataTable customersTable = worksheet.ExportDataTable(worksheet.UsedRange, ExcelExportDataTableOptions.ColumnNames);
 
  //Load the data to dataGrid
  dataGrid.DataSource = customersTable;
 
  //No exception will be thrown if there are unsaved workbooks.
  excelEngine.ThrowNotSavedOnDestroy = false;
}

 

VB.NET

'Initialize the Excel Engine
Using excelEngine As ExcelEngine = New ExcelEngine()
 
  'Initialize Application
  Dim application As IApplication = excelEngine.Excel
 
  'Set default version for application
  application.DefaultVersion = ExcelVersion.Excel2013
 
  'Open existing workbook with data entered
  Dim assembly As Assembly = GetType(Form1).GetTypeInfo.Assembly
  Dim fileStream As Stream = assembly.GetManifestResourceStream("ExceltoDataGrid.Sample.xlsx")
  Dim workbook As IWorkbook = application.Workbooks.Open(fileStream)
 
  'Accessing first worksheet in the workbook
  Dim worksheet As IWorksheet = workbook.Worksheets(0)
 
  'Export data from Excel worksheet
  Dim customersTable As DataTable = worksheet.ExportDataTable(worksheet.UsedRange, ExcelExportDataTableOptions.ColumnNames)
 
  'Load the data to dataGrid
  DataGrid.DataSource = customersTable
 
  'No exception will be thrown if there are unsaved workbooks.
  excelEngine.ThrowNotSavedOnDestroy = False
 
End Using

 

A complete Windows Forms working example of how to load data from the input Excel to DataGrid or DataGridView in C# can be downloaded from Excel to DataGrid or DataGridView.zip.

By executing the program, you will get the output Excel file as shown below:

Load Excel to DataGrid

Refer here to explore the rich set of Syncfusion Excel (XlsIO) library features.

See Also:

Export DataTable to Excel in C#, VB.NET

Create Excel from DataTable in C#, VB.NET

How to export data from Excel file to DataTable

How to export DataTable with images to Excel in C#, VB.NET?

How to preserve data types during ImportDataTable in C#, VB.NET?

Export DataTable to Excel with formatting in C#, VB.NET

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon