5 Replies - 259 Views - Last Post: 29 August 2011 - 07:05 AM Rate Topic: -----

#1 mira-magdy  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 22-August 11

problem in the datagridview

Posted 28 August 2011 - 01:12 AM

  OpenFileDialog of = new OpenFileDialog();
            of.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
            if (of.ShowDialog() == DialogResult.OK)
            {
                objstream = new StreamReader(of.FileName);

                OleDbConnection ExcelConection = null;
                OleDbDataAdapter ExcelCommand = null;



                try
                {
                    //OleStringBuilder =new OleDbConnectionStringBuilder(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';");
                    string conn = @"Provider=Microsoft.jet.OLEDB.4.0;" +
                    @"Data Source=" + of.FileName + ";" +
                    @"Extended Properties=" + Convert.ToChar(34).ToString() + ";" +
                    @"Excel 12.0;HDR=Yes;IMEX=1" + Convert.ToChar(34).ToString();

                    ExcelConection = new OleDbConnection(conn);

                    ExcelCommand = new OleDbDataAdapter("Select * From [Sheet1$]", ExcelConection);
                    ExcelCommand.TableMappings.Add("table", "testtable");
                    DataSet ds = new DataSet();
                    ExcelCommand.Fill(ds);
                    dataGridView1.DataSource = ds.Tables[0];
                    ExcelConection.Close();



                    //Grid.DataBind();
                    ExcelConection.Close();
                }
                catch (Exception etc)
                {
                    MessageBox.Show("error" + etc.Message);
                }
                
            }
        }
    }


i had a problem to show the excel in a datagrid when it reach to step to fill the data in the grid it jump to catch and didn't show the data in the table to the grid .

This post has been edited by mira-magdy: 28 August 2011 - 01:18 AM


Is This A Good Question/Topic? 0
  • +

Replies To: problem in the datagridview

#2 rgfirefly24  Icon User is online

  • D.I.C Lover
  • member icon


Reputation: 233
  • View blog
  • Posts: 1,310
  • Joined: 07-April 08

Re: problem in the datagridview

Posted 28 August 2011 - 08:57 AM

what is the error you are getting?
Was This Post Helpful? 0
  • +
  • -

#3 mira-magdy  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 22-August 11

Re: problem in the datagridview

Posted 29 August 2011 - 04:27 AM

system.data.oledb.oledbexception 0x80004005
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5667
  • View blog
  • Posts: 22,511
  • Joined: 23-August 08

Re: problem in the datagridview

Posted 29 August 2011 - 05:08 AM

Did you try searching Google for your error?
Was This Post Helpful? 0
  • +
  • -

#5 mira-magdy  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 22-August 11

Re: problem in the datagridview

Posted 29 August 2011 - 05:31 AM

View PostJackOfAllTrades, on 29 August 2011 - 05:08 AM, said:



of course i tried many times and try several solutions but i get nothing
Was This Post Helpful? 0
  • +
  • -

#6 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 83
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: problem in the datagridview

Posted 29 August 2011 - 07:05 AM

How about this:
       private void readToDgv()
        {
            StreamReader objstream;
OpenFileDialog of = new OpenFileDialog();
          of.Filter = "Excel files (*.xls*)|*.xls*|All files (*.*)|*.*";
of.FilterIndex=1;
          if (of.ShowDialog() == DialogResult.OK)
          {
              objstream = new StreamReader(of.FileName);

              OleDbConnection ExcelConection = null;
              OleDbDataAdapter ExcelCommand = null;

              try
              {
                  string conn = "Provider=Microsoft.ACE.OLEDB.12.0;" + @"Data Source=" + of.FileName + ";" + "Extended Properties=\"Excel 12.0;HDR=YES\";";
                  ExcelConection = new OleDbConnection(conn);
                  ExcelCommand = new OleDbDataAdapter("Select * From [Sheet1$]", conn);
                  DataTable dt= new DataTable("testtable");
                  ExcelCommand.TableMappings.Add("Table", "testtable");//<-- table name is case-sensitive
                  DataSet ds = new DataSet();
                  ExcelCommand.Fill(ds,"testtable");
                  dataGridView1.DataSource = ds.Tables["testtable"];
                  ExcelConection.Close();

                  ExcelConection.Close();
              }
              catch (Exception etc)
              {
                  MessageBox.Show("error" + etc.Message + "\n" + etc.StackTrace);
              }
              
          }
      }

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1