Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. importing RowName and ColumnName from an excelfile to a datagridview ...

importing RowName and ColumnName from an excelfile to a datagridview ...

Scheduled Pinned Locked Moved C#
helpdatabase
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nassimnastaran
    wrote on last edited by
    #1

    Hi All ! I want to Import an Excel File to a DataGridView as FirstRow (Except A1 Cell that's null)will be Column HeaderCell in DataGridView and First Column in Excel file (Except Cell of A1=null) will be Row HeaderCell in DataGridView .Here is my code :

    string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0;HDR=YES;IMEX=1;""", openFileDialog1.FileName);
    string query = String.Format("select * from [{0}$]", SheetName);
    OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);

    DataSet ds = new DataSet();

    dataAdapter.Fill(ds);
    dataGridView1.DataSource = ds.Tables[0];

    int row = ds.Tables[0].Rows.Count;
    int col = ds.Tables[0].Columns.Count;

    for (int i = 0; i < row - 1; i++)
    {
    dataGridView1.Rows[i].HeaderCell.Value = ds.Tables[0].Rows[i].ToString(); ;
    }

    for (int j = 0; j < col - 1; j++)
    {
    dataGridView1.Columns[j].HeaderCell.Value = ds.Tables[0].Columns[j].ToString();
    }

    But I have some Problem for Header Column And Header Row . Thanks For Any Help .

    M 1 Reply Last reply
    0
    • N nassimnastaran

      Hi All ! I want to Import an Excel File to a DataGridView as FirstRow (Except A1 Cell that's null)will be Column HeaderCell in DataGridView and First Column in Excel file (Except Cell of A1=null) will be Row HeaderCell in DataGridView .Here is my code :

      string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0;HDR=YES;IMEX=1;""", openFileDialog1.FileName);
      string query = String.Format("select * from [{0}$]", SheetName);
      OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);

      DataSet ds = new DataSet();

      dataAdapter.Fill(ds);
      dataGridView1.DataSource = ds.Tables[0];

      int row = ds.Tables[0].Rows.Count;
      int col = ds.Tables[0].Columns.Count;

      for (int i = 0; i < row - 1; i++)
      {
      dataGridView1.Rows[i].HeaderCell.Value = ds.Tables[0].Rows[i].ToString(); ;
      }

      for (int j = 0; j < col - 1; j++)
      {
      dataGridView1.Columns[j].HeaderCell.Value = ds.Tables[0].Columns[j].ToString();
      }

      But I have some Problem for Header Column And Header Row . Thanks For Any Help .

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Importing via an OLEDB connection only works on a well formed excel file/table. If you are having trouble then it is highly likely that the excel file is crap (as usual). Throw a break point after the fill and see if there is some data.

      Never underestimate the power of human stupidity RAH

      N 1 Reply Last reply
      0
      • M Mycroft Holmes

        Importing via an OLEDB connection only works on a well formed excel file/table. If you are having trouble then it is highly likely that the excel file is crap (as usual). Throw a break point after the fill and see if there is some data.

        Never underestimate the power of human stupidity RAH

        N Offline
        N Offline
        nassimnastaran
        wrote on last edited by
        #3

        Thanks for your reply , Here is my file , pl z check it . or : www.4shared.com/rar/6WxLLxtL/ReadFromExcelFile.html I've prob yet ! thanks!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups