ByteScout Barcode Suite - C# - Import from winforms datagrid with spreadsheet sdk - ByteScout
Announcement
Our ByteScout SDK products are sunsetting as we focus on expanding new solutions.
Learn More Open modal
Close modal
Announcement Important Update
ByteScout SDK Sunsetting Notice
Our ByteScout SDK products are sunsetting as we focus on our new & improved solutions. Thank you for being part of our journey, and we look forward to supporting you in this next chapter!

ByteScout Barcode Suite – C# – Import from winforms datagrid with spreadsheet sdk

  • Home
  • /
  • Articles
  • /
  • ByteScout Barcode Suite – C# – Import from winforms datagrid with spreadsheet sdk

How to import from winforms datagrid with spreadsheet sdk in C# using ByteScout Barcode Suite

Learning is essential in computer world and the tutorial below will demonstrate how to import from winforms datagrid with spreadsheet sdk in C#

The coding instructions are formulated to help you to try-out the features without the requirement to write your own code. ByteScout Barcode Suite is the set that includes three different SDK products to generate barcodes, read barcodes and read and write spreadsheets: Barcode SDK, Barcode Reader SDK and Spreadsheet SDK. It can be applied to import from winforms datagrid with spreadsheet sdk using C#.

The SDK samples given below describe how to quickly make your application do import from winforms datagrid with spreadsheet sdk in C# with the help of ByteScout Barcode Suite. Just copy and paste the code into your C# application’s code and follow the instructions. Further improvement of the code will make it more robust.

If you want to try other source code samples then the free trial version of ByteScout Barcode Suite is available for download from our website. Just try other source code samples for C#.

On-demand (REST Web API) version:
 Web API (on-demand version)

On-premise offline SDK for Windows:
 60 Day Free Trial (on-premise)

Form1.Designer.cs
      
namespace ImportExportFromWinFormsDataGrid.CSharp { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.dataGrid1 = new System.Windows.Forms.DataGrid(); this.dataGridTableStyle1 = new System.Windows.Forms.DataGridTableStyle(); this.dataGridTextBoxColumn1 = new System.Windows.Forms.DataGridTextBoxColumn(); this.btnImport = new System.Windows.Forms.Button(); this.btnExport = new System.Windows.Forms.Button(); this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); ((System.ComponentModel.ISupportInitialize) (this.dataGrid1)).BeginInit(); this.SuspendLayout(); // // dataGrid1 // this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles) ((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.dataGrid1.DataMember = ""; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Location = new System.Drawing.Point(12, 12); this.dataGrid1.Name = "dataGrid1"; this.dataGrid1.Size = new System.Drawing.Size(438, 308); this.dataGrid1.TabIndex = 0; this.dataGrid1.TableStyles.AddRange(new System.Windows.Forms.DataGridTableStyle[] { this.dataGridTableStyle1}); // // dataGridTableStyle1 // this.dataGridTableStyle1.DataGrid = this.dataGrid1; this.dataGridTableStyle1.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[] { this.dataGridTextBoxColumn1}); this.dataGridTableStyle1.HeaderForeColor = System.Drawing.SystemColors.ControlText; // // dataGridTextBoxColumn1 // this.dataGridTextBoxColumn1.Format = ""; this.dataGridTextBoxColumn1.FormatInfo = null; this.dataGridTextBoxColumn1.NullText = ""; this.dataGridTextBoxColumn1.Width = 75; // // btnImport // this.btnImport.Anchor = ((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.btnImport.Location = new System.Drawing.Point(238, 326); this.btnImport.Name = "btnImport"; this.btnImport.Size = new System.Drawing.Size(212, 23); this.btnImport.TabIndex = 1; this.btnImport.Text = "Import from DataGrid to spreadsheet"; this.btnImport.UseVisualStyleBackColor = true; this.btnImport.Click += new System.EventHandler(this.btnImport_Click); // // btnExport // this.btnExport.Anchor = ((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.btnExport.Location = new System.Drawing.Point(238, 355); this.btnExport.Name = "btnExport"; this.btnExport.Size = new System.Drawing.Size(212, 23); this.btnExport.TabIndex = 2; this.btnExport.Text = "Export from spreadsheet to DataGrid"; this.btnExport.UseVisualStyleBackColor = true; this.btnExport.Click += new System.EventHandler(this.btnExport_Click); // // openFileDialog1 // this.openFileDialog1.FileName = "openFileDialog1"; this.openFileDialog1.Filter = "Excel files (*.xls;*.xlsx)|*.xls;*.xlsx|All files|*.*"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(462, 390); this.Controls.Add(this.btnExport); this.Controls.Add(this.btnImport); this.Controls.Add(this.dataGrid1); this.Name = "Form1"; this.Text = "ImportFromWinFormsDataGrid"; ((System.ComponentModel.ISupportInitialize) (this.dataGrid1)).EndInit(); this.ResumeLayout(false); } #endregion private System.Windows.Forms.DataGrid dataGrid1; private System.Windows.Forms.Button btnImport; private System.Windows.Forms.Button btnExport; private System.Windows.Forms.OpenFileDialog openFileDialog1; private System.Windows.Forms.DataGridTableStyle dataGridTableStyle1; private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn1; } }

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit ByteScout Barcode Suite Home Page

Explore ByteScout Barcode Suite Documentation

Explore Samples

Sign Up for ByteScout Barcode Suite Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

Form1.cs
      
using System; using System.Data; using System.Diagnostics; using System.IO; using System.Windows.Forms; using Bytescout.Spreadsheet; namespace ImportExportFromWinFormsDataGrid.CSharp { public partial class Form1 : Form { public Form1() { InitializeComponent(); dataGrid1.DataSource = GetDemoDataTable(); } private void btnImport_Click(object sender, EventArgs e) { Cursor = Cursors.WaitCursor; try { const string fileName = "CSharpImportFromDataTable.xls"; // retrieve DataTable from DataGrid DataTable dataTable = (DataTable) dataGrid1.DataSource; // Create spreadsheet Spreadsheet spreadsheet = new Spreadsheet(); // Import data from DataTable into spreadheet spreadsheet.ImportFromDataTable(dataTable); // Insert row with column captions Worksheet worksheet = spreadsheet.Worksheets[0]; worksheet.Rows.Insert(0); for (int colIndex = 0; colIndex < dataTable.Columns.Count; colIndex++) { worksheet.Cell(0, colIndex).Value = dataTable.Columns[colIndex].Caption; } // Save the spreadsheet if (File.Exists(fileName)) File.Delete(fileName); spreadsheet.SaveAs(fileName); // Close spreadsheet spreadsheet.Close(); // Open the spreadsheet Process.Start(fileName); } finally { Cursor = Cursors.Default; } } private void btnExport_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { Cursor = Cursors.WaitCursor; try { // Create spreadsheet Spreadsheet spreadsheet = new Spreadsheet(); // Load spreadsheet from file spreadsheet.LoadFromFile(openFileDialog1.FileName); // Export to DataGrid DataTable dataTable = spreadsheet.ExportToDataTable(); dataGrid1.DataSource = dataTable; } finally { Cursor = Cursors.Default; } } } /// <summary> /// Fills a data table of the periodic table of elements. /// </summary> private DataTable GetDemoDataTable() { DataTable periodicTable = new DataTable("PeriodicTable"); periodicTable.Columns.Add("Name", typeof(string)); periodicTable.Columns.Add("Symbol", typeof(string)); periodicTable.Columns.Add("AtomicNumber", typeof(int)); DataRow dr = periodicTable.Rows.Add(); dr[0] = "Hydrogen"; dr[1] = "H"; dr[2] = "1"; dr = periodicTable.Rows.Add(); dr[0] = "Helium"; dr[1] = "He"; dr[2] = "2"; dr = periodicTable.Rows.Add(); dr[0] = "Lithium"; dr[1] = "Li"; dr[2] = "3"; dr = periodicTable.Rows.Add(); dr[0] = "Beryllium"; dr[1] = "Be"; dr[2] = "4"; dr = periodicTable.Rows.Add(); dr[0] = "Boron"; dr[1] = "B"; dr[2] = "5"; dr = periodicTable.Rows.Add(); dr[0] = "Carbon"; dr[1] = "C"; dr[2] = "6"; return periodicTable; } } }

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit ByteScout Barcode Suite Home Page

Explore ByteScout Barcode Suite Documentation

Explore Samples

Sign Up for ByteScout Barcode Suite Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

Program.cs
      
using System; using System.Windows.Forms; namespace ImportExportFromWinFormsDataGrid.CSharp { class Program { [STAThread] static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit ByteScout Barcode Suite Home Page

Explore ByteScout Barcode Suite Documentation

Explore Samples

Sign Up for ByteScout Barcode Suite Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

VIDEO

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit ByteScout Barcode Suite Home Page

Explore ByteScout Barcode Suite Documentation

Explore Samples

Sign Up for ByteScout Barcode Suite Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

Tutorials:

prev
next