CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2008
    Posts
    2

    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();
    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 .
    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 :
    http://www.4shared.com/rar/6WxLLxtL/...ExcelFile.html
    Thx for any help !

  2. #2
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    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
  •  





Click Here to Expand Forum to Full Width

Featured