Click here to Skip to main content
13,043,898 members (47,680 online)
Rate this:
Please Sign up or sign in to vote.
See more: , +

I want to import specific cells of an Excel file to specific cells of a datagridview.

I followed many tutorials here that import Excel tables, but as I want only specific cells in specific locations of the datagridview, I adapted the code.

Instead of: DataGridView1.DataSource = dt; I did: dataGridView1.Rows[0].Cells[0].Value = dt; but it doesn’t work.

Can anyboby help me please?

Thank you.

Here is the code:

 string constr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + textBox1.Text + @";Extended Properties=""Excel 12.0 Macro;HDR=YES;ImportMixedTypes=Text;TypeGuessRows=0""";
            OleDbConnection con = new OleDbConnection(constr);
            //specific cell 1 - product "code"
            OleDbDataAdapter sda = new OleDbDataAdapter("Select * From [ARTIGOS$A" + textBox3.Text + ":A" + textBox3.Text + "]", con);
            //specific cell 2 - product "description"
            OleDbDataAdapter sdb = new OleDbDataAdapter("select * From [ARTIGOS$B" + textBox3.Text + ":B" + textBox3.Text + "]", con);
            //specific cell 3 - product "price"
            OleDbDataAdapter sdc = new OleDbDataAdapter("Select * From [ARTIGOS$I" + textBox3.Text + ":I" + textBox3.Text + "]", con);
            DataTable dt = new DataTable();
            DataTable dt2 = new DataTable();
            DataTable dt3 = new DataTable();
            dataGridView1.Rows[0].Cells[0].Value = dt;
            dataGridView1.Rows[0].Cells[1].Value = dt2;
            dataGridView1.Rows[0].Cells[2].Value = textBox2.Text;
            dataGridView1.Rows[0].Cells[3].Value = dt3;
Posted 17-Apr-13 0:15am
You can't add a datatable object into a value inside a cell.
You can bind a datatable into dataGridView.DataSource property.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Using OleDb it's not possible!

Steps to do:
1) Create new instance of MS Excel
2) Open workbook
3) Get data from worksheet and insert into DataGridView
4) Close workbok
5) Close application

See examples:[^][^]
Datagridview to Excel C#[^]
Hi Maciej, why it is not possible doing it using OleDb?
Maciej Los 18-Apr-13 17:12pm
OleDb driver provides methods to read tabular format data. As i see, you want to read specific Excel ranges: A...:A..., B...:B.... OleDb driver reads data in continuos way, usually UsedRange.

You can't set values like that: dataGridView1.Rows[0].Cells[1].Value = dt2;, because datatable object is a set of records.
BTW, dataGridView1 has DataSource property: dataGridView1.DataSource = dt2;
Please, read msdn documentation.
Thanks for the reply. OleDb gets the job done in the case presented by ricardo. On the subject of DataSource I did not understand, because my comment was exactly that.
Maciej Los 19-Apr-13 2:03am
Sorry, yesterday, i was a bit tired. ;)
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I would consider Excel Interopt:[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 18 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100