Friday, 16 May 2014

How to import Excel File to DataGridView in window application



                                                                                                                                              Previous..
                                                                                                                                                  Next..
Here i will explain How to import Excel File to DataGridView in window application.Before this i explain how to create excel file in window application.

Step(1) :Form
Drag and down a DataGridView and 2 button. One for Choose File and other for Loading data into DataGridView, And 2 textBox.

Step(2) :Code
Double click on "Choose Button" and  "Load excel" write this code. In tutorial 24 i write this code.


private void Choose_Btn_click(object sender, EventArgs e)
{
OpenFileDialog openfile = new OpenFileDialog ();

if(openfile .ShowDialog() ==System.Window.Forms.DialogResult.OK)
{
this.textBox1.Text = openfile .FileName;

}

you add some namespace here.
using system.data.oleDB;
using System.data.sqlClient;

and code on button click on Load Excel Button.
private void LoadExcel_btn_click(object sender, EventArgs e)
{
string Pathconn = "Provider = Microsoft.Jet.OLEDB.4.0; DataSource = "+textBox1.Text+" ; Extended properties=\"Excel 8.0;HDR=YES;\";";

oledebconnection  con =  new oledebconnection ( Pathconn);

OledbDataAdapter MydaAtadapter =  new OledbDataAdapter ( "Select * from [ "+TextBox2.Text+ "$]", con;
DataTable dt = new DataTable ();

MydaAtadapter .Fill(dt);

dataGridView1.DataSource = dt;

}

So Code Is
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using ExcelLibrary.CompoundDocumentFormat;
using ExcelLibrary.SpreadSheet;

using ExcelLibrary.BinaryDrawingFormat;
using ExcelLibrary.BinaryFileFormat;

using system.data.oleDB;

using System.data.sqlClient;

using System.Text;
using System.IO;

namespace First_Csharp_app
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
private void Choose_Btn_click(object sender, EventArgs e)
{
OpenFileDialog openfile = new OpenFileDialog ();

if(openfile .ShowDialog() ==System.Window.Forms.DialogResult.OK)
{
this.textBox1.Text = openfile .FileName;

}

private void LoadExcel_btn_click(object sender, EventArgs e)
{
string Pathconn = "Provider = Microsoft.Jet.OLEDB.4.0; DataSource = "+textBox1.Text+" ; Extended properties=\"Excel 8.0;HDR=YES;\";";

oledebconnection  con =  new oledebconnection ( Pathconn);

OledbDataAdapter MydaAtadapter =  new OledbDataAdapter ( "Select * from [ "+TextBox2.Text+ "$]", con;
DataTable dt = new DataTable ();

MydaAtadapter .Fill(dt);

dataGridView1.DataSource = dt;

}

Step(3) :OutPut
Now Run your application

                                                                                                                                              Previous..
                                                                                                                                                  Next..

2 comments:

  1. can i import image from excel file into data grid view ?

    ReplyDelete
    Replies
    1. Yes you can import.. for this please refer this link

      http://csharp.net-informations.com/excel/csharp-insert-picture-excel.htm

      Delete

C# program Selection Sorting

Selection sort is a straightforward sorting algorithm. This algorithm search for the smallest number in the elements array and then swap i...