Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi!

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:

C#
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();

           sda.Fill(dt);
           sdb.Fill(dt2);
           sdc.Fill(dt3);

           dataGridView1.Rows.Add();
           dataGridView1.Rows[0].Cells[0].Value = dt;
           dataGridView1.Rows[0].Cells[1].Value = dt2;
           dataGridView1.Rows.Add();
           dataGridView1.Rows[0].Cells[2].Value = textBox2.Text;
           dataGridView1.Rows[0].Cells[3].Value = dt3;
Posted
Comments
José Amílcar Casimiro 18-Apr-13 16:41pm    
You can't add a datatable object into a value inside a cell.
You can bind a datatable into dataGridView.DataSource property.

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:
http://www.dotneter.com/reading-excel-and-binding-to-datagridview-using-microsoft-office-interop-excel[^]
http://stackoverflow.com/questions/11518344/excel-to-datagridview-without-using-hard-code[^]
Datagridview to Excel C#[^]
 
Share this answer
 
Comments
José Amílcar Casimiro 18-Apr-13 16:42pm    
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.
José Amílcar Casimiro 18-Apr-13 17:24pm    
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. ;)
José Amílcar Casimiro 19-Apr-13 4:56am    
:)
I would consider Excel Interopt: http://www.dotnetperls.com/excel[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900