Skip to content

Displaying Excel file in apllication.

tommy

Asked Feb 8, 2009, 9:40 PM EST2 answers
Share
LinkedInX

Hi, I'm trying to display an excel file inside the application datagridview using this code...

Java
try
        {
            string connString = @"provider=Microsoft.Jet.OLEDB.4.0;data source=C:\Documents and Settings\Desktop\workbooktest.xls;Extended Properties=""Excel 8.0;HDR=YES;""";

            OleDbConnection oledbConn = new OleDbConnection(connString);
            oledbConn.Open();
            OleDbCommand cmd = new OleDbCommand("SELECT * FROM [location$]", oledbConn);
            OleDbDataAdapter oleda = new OleDbDataAdapter();
            oleda.SelectCommand = cmd;
            DataSet ds = new DataSet();
            oleda.Fill(ds);
            GridView.DataSource = ds.Tables[0].DefaultView;
            GridView.DataBind();
            oledbConn.Close();
        }

but this compiling error kept coming up...

'System.Windows.Forms.DataGridView' does not contain a definition for 'DataBind' and no extension method 'DataBind' accepting a first argument of type 'System.Windows.Forms.DataGridView' could be found (are you missing a using directive or an assembly reference?) 

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    anyone have any idea bout this...I'm still stuck with it.
    0
  • 0

    got it working with the following code:

    Java
                try
                {
                    string connString = @"provider=Microsoft.Jet.OLEDB.4.0;data source=C:\Documents and Settings\Desktop\workbooktest.xls;Extended Properties=""Excel 8.0;HDR=YES;""";
                    OleDbConnection oledbConn = new OleDbConnection(connString);
                    oledbConn.Open();
                    OleDbCommand cmd = new OleDbCommand("SELECT * FROM [location$A2:O33]", oledbConn);
                    OleDbDataAdapter oleda = new OleDbDataAdapter();
                    oleda.SelectCommand = cmd;
                    DataSet ds = new DataSet();
                    oleda.Fill(ds);
                    dataGridView1.DataSource = ds.Tables[0].DefaultView;
                    //dataGridView1.DataBind();
                    oledbConn.Close();
                }

    Is there a way to make the excel file read-only and uneditable in the application?

    0