Excel to DataGrid or DataGridView in C#.
Syncfusion® Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. It 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 new Windows Forms application
Step 2: Name the application.
Name the application
Step 3: Install the Syncfusion.XlsIO.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org.
Install NuGet package
Step 4: Include the following namespaces in the Form1.cs file.
C#
using Syncfusion.XlsIO;
VB.NET
Imports Syncfusion.XlsIO
Step 5: Add the below code in Form1.Designer.cs file for a new button.
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" AddHandler btnImport.Click, AddressOf Me.btnImport_Click '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: Data in the Excel worksheet can be exported to a DataTable using the ExportDataTable() method. This data table can be assigned as source to data grid. Add the below complete code in the button click event in Form1.cs file to export data from Excel worksheet to DataGrid.
C#
// Initialize the Excel Engine using (ExcelEngine excelEngine = new ExcelEngine()) { // Initialize Application IApplication application = excelEngine.Excel; // Set default version for application. application.DefaultVersion = ExcelVersion.Xlsx; // 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 the 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.Xlsx '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 the 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
The input Excel document (Sample.xlsx) should be added in the project and made an Embedded Resource in the Build Action.
Input file as Embedded Resource
A complete working example of how to load data from Excel worksheet to DataGrid in C# can be downloaded from Excel to DataGrid.zip.
By executing the program, you will get the DataGrid loaded as 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
Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about excel to DataGrid or DataGridView in C#.
You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion® components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!