views:

173

answers:

1

I am trying to bind excel file to dataGridView

strConn = "Provider=Microsoft.Jet.OLEDB.4.0;"
                + "Data Source=E://Org.xls;" 
                + "Extended Properties=" + (char)34 
                + "Excel 8.0;HDR=Yes;" + (char)34;
            OleDbConnection conn = new OleDbConnection(strConn);
            textBox1.Text = "test";
            OleDbCommand command = new OleDbCommand("Select * from [Sheet1$]", conn);
            conn.Open();
            dataGridView1.DataSource = command.ExecuteReader(); 



            conn.Close();

But grid view doesn't show anything. It doesn't give error either

+1  A: 

Heres how to do it, just need to change the path for the excel file and the reference to the grid.

    OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c://Org.xls;Extended Properties=" + (char)34 + "Excel 8.0;HDR=Yes;" + (char)34);
    DataSet myExcelData=new DataSet();

    conn.Open();

    OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * from [Sheet1$]", conn);
    myDataAdapter.Fill(myExcelData);

    ultraGrid1.DataSource = myExcelData;

    conn.Close(); 
David
still giving me the blank view
Sphinx
myExcelData.tables[0] ..............worked for me thanks
Sphinx
Hi guys, what the prerequisites for the client machine (do they have to install Microsoft Excel?) if I bind Excel data to DataGridView. I'm thinking of using pure text file for saving and editing data, but it's very tedious and difficult to deal with. Still, if there is no special client-environment requirements, I'm gonna use Excel for saving data, and use DataGridView to import (from Excel) and export data (to Excel).
Peter Lee
What if I only use CSV file? Does the client machine have to be installed with something special?
Peter Lee