Click here to Skip to main content
11,704,315 members (43,116 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Excel DataGridView , +
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:

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 17-Apr-13 0:15am
Comments
José Amílcar Ferreira Casimiro at 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.
Rate this: bad
good
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:
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#[^]
  Permalink  
Comments
José Amílcar Ferreira Casimiro at 18-Apr-13 16:42pm
   
Hi Maciej, why it is not possible doing it using OleDb?
Maciej Los at 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 Ferreira Casimiro at 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 at 19-Apr-13 2:03am
   
Sorry, yesterday, i was a bit tired. ;)
   
:)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I would consider Excel Interopt: http://www.dotnetperls.com/excel[^]
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 740
1 OriginalGriff 520
2 Maciej Los 355
3 Andy Lanng 284
4 CPallini 275
0 OriginalGriff 8,824
1 Sergey Alexandrovich Kryukov 8,247
2 CPallini 5,197
3 Maciej Los 4,726
4 Mika Wendelius 3,606


Advertise | Privacy | Mobile
Web03 | 2.8.150819.1 | Last Updated 18 Apr 2013
Copyright © CodeProject, 1999-2015
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