Import Data from Excel to DataGridView in C++.net
I need an example please, if anyone can help me with this...
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
JUAN AGUIPosted May 1, 2012, 4:19 PM
Don't forget to declare this at the begin
using namespace System::Data::OleDb;
Satyapriya NayakPosted Apr 30, 2012, 10:51 PM
Here is an example in Csharp
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Retrieving_excel_data_to_combobox
{
public partial class Form1 : Form
{
public OleDbConnection con;
public void pintu(string s)
{
con = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; " + "data source='" + s + " '; " + "Extended Properties=Excel 8.0;");
}
public OleDbCommand com;
public DataSet ds;
public OleDbDataAdapter oledbda;
public DataTable dt;
public string str;
public Form1()
{
InitializeComponent();
}
private void btnbrowse_Click(object sender, EventArgs e)
{
OpenFileDialog openfiledialog1 = new OpenFileDialog();
openfiledialog1.ShowDialog();
openfiledialog1.Filter = "allfiles|*.xls";
TextBox1.Text = openfiledialog1.FileName;
}
private void btndisplay_Click(object sender, EventArgs e)
{
pintu(TextBox1.Text);
try
{
con.Open();
str = "select * from [sheet1$]";
com = new OleDbCommand(str, con);
ds = new DataSet();
oledbda = new OleDbDataAdapter(com);
oledbda.Fill(ds, "[sheet1$]");
con.Close();
DataGridView1.DataSource = ds;
DataGridView1.DataMember = "[sheet1$]";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
Thanks
George ericPosted Apr 30, 2012, 10:38 PM