CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 2 of 2

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

  1. #1
    Join Date
    May 2008

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

    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 SheetName = "SAW";
    DialogResult dr = this.openFileDialog1.ShowDialog();

    if (dr == System.Windows.Forms.DialogResult.OK)

    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();
    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 .
    in this importing , I must have only Col1,Co2 as a Header Column and row1,row2 must be as a HeaderCell Row In DataGridView .
    You Can find my sample in :
    Thx for any help !

  2. #2
    Join Date
    Mar 2005
    Vienna, Austria

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

    sorry but no one is interested to download an exe file. If you want help zip the project after removing all data in the bin and obj folder so there is no exe dile included into your project and add it to this thread. so everyone can have a look into it
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)