Header Ads Widget

Responsive Advertisement

Import Data from Excel to Datagridview in C# Window Form with Source Code



Source Code Helpful?

That's project is helpful for those students that's wants to develop / create your on project in C# window form that has totally professional level development criteria.

That's source code has following control functions:

  • Who import data from excel sheet to dataGridView?

Source Code:


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 ImportDataDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnChoseFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Chose Excel File";
            ofd.Filter = "Excel File(*.xls)| *.xls";
           if (ofd.ShowDialog() == DialogResult.OK)
            {
                label1.Text = ofd.FileName;
            }
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            try
            {
                string copath = "Provider=Microsoft.Jet.OleDB.4.0;Data Source=" + label1.Text + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";";
                OleDbConnection con = new OleDbConnection(copath);
                OleDbDataAdapter adp = new OleDbDataAdapter("Select * From [" + textBox1.Text + "$]",con);
                DataTable dt = new DataTable();
                adp.Fill(dt);
                dataGridView1.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}





At YouTube Live Video:





Use Mobile Phone and Scan for direct access to link on mobile phone.



Post a Comment

0 Comments