0% found this document useful (0 votes)
41 views2 pages

Csharp Datagridview Load Excel

The document contains a C# Windows Forms application code that connects to an Excel file using OleDb. It retrieves data from the first sheet of the Excel file and displays it in a DataGridView when a button is clicked. The connection to the Excel file is established using a specified connection string and the data is loaded into a DataSet before being assigned to the DataGridView's data source.

Uploaded by

Aurel Borcan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views2 pages

Csharp Datagridview Load Excel

The document contains a C# Windows Forms application code that connects to an Excel file using OleDb. It retrieves data from the first sheet of the Excel file and displays it in a DataGridView when a button is clicked. The connection to the Excel file is established using a specified connection string and the data is loaded into a DataSet before being assigned to the DataGridView's data source.

Uploaded by

Aurel Borcan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

using System;

using System.Data;

using System.Windows.Forms;

using System.Data.SqlClient;

namespace WindowsFormsApplication1

public partial class Form1 : Form

public Form1()

InitializeComponent();

private void button1_Click(object sender, EventArgs e)

System.Data.OleDb.OleDbConnection MyConnection;

System.Data.DataSet DtSet;

System.Data.OleDb.OleDbDataAdapter MyCommand;

MyConnection = new
System.Data.OleDb.OleDbConnection(@"provider=Microsoft.Jet.OLEDB.4.0;Data
Source='c:\csharp.net-informations.xls';Extended Properties=Excel 8.0;");

MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from


[Sheet1$]", MyConnection);

MyCommand.TableMappings.Add("Table", "Net-informations.com");

DtSet = new System.Data.DataSet();

MyCommand.Fill(DtSet);

dataGridView1.DataSource = DtSet.Tables[0];

MyConnection.Close();

}
}

You might also like