Transfering data from Excel to dataGridView

Posted by Panecillo on Stack Overflow See other posts from Stack Overflow or by Panecillo
Published on 2011-11-21T01:43:32Z Indexed on 2011/11/21 1:51 UTC
Read the original article Hit count: 132

Filed under:
|

I have a problem when I want to transfer data from Excel to dataGridView in C#.

My Excel's column has numeric and alphanumeric values. But for example, if the column has 3 numbers and 2 alphanumeric values then only the numbers are shown in the dataGridView, and vice versa. Why aren't all the values shown? The next is what happen:

Excel's Column:                   DataGridView's Column:
   45654                               45654
   P745K 
   31233                               31233
   23111                               23111
   45X2Y

Here is my code to load the dataGridView:

string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\test.xls;Extended Properties=""Excel 8.0;HDR=YES;""";

        DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");

        DbDataAdapter adapter = factory.CreateDataAdapter();

        DbCommand selectCommand = factory.CreateCommand();
        selectCommand.CommandText = "SELECT * FROM [sheet1$]";

        DbConnection connection = factory.CreateConnection();
        connection.ConnectionString = connectionString;

        selectCommand.Connection = connection;

        adapter.SelectCommand = selectCommand;

        data = new DataSet();

        adapter.Fill(data);

        dataGridView1.DataSource = data.Tables[0].DefaultView;

I hope I explained it well. Sorry my bad english.

Thanks.

© Stack Overflow or respective owner

Related posts about c#

Related posts about excel