Click here to Skip to main content
13,595,773 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have Uploaded a Excel file and viewed in Datagridview C#.My Query is during run time in header i'm getting F1,F2,F3 like this...How to get the column name instead of getting F1,F2,F3...

Posted 26-Nov-12 17:59pm
Krunal Rohit 27-Nov-12 0:36am
Show your code..
Balamurugan1989 27-Nov-12 2:05am
OleDbConnection theConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;data source=books.xls;Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1;\"");
OleDbDataAdapter theDataAdapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", theConnection);
DataSet theDS = new DataSet();
dt = new DataTable();
this.dataGridView1.DataSource = dt;
R_Mahesh 27-Nov-12 3:52am
here in this code for connection string you have set the HDR=NO so set it to YES you will get the headings from Excel file...
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

hi you can see this link[^]

just put the headings in your Excel file for each column

See one more link[^]
Balamurugan1989 27-Nov-12 1:35am
I'm working in C# windows form application.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Take one TextBox In the property (Name)=txtPath
OpenFileDialog Property (Name)=ofd
one Button Property (Name)=btnBrows
two RadioButton 1st Property (Name)= rdbY 2nd Property (Name)=rdbN
one DtaGridView Property (Name)=dataGridView1

Now see the code --.cs

private void btnBrows_Click(object sender, EventArgs e)
            DialogResult result = ofd.ShowDialog(); // Show the dialog.
            if (result == DialogResult.OK) // Test result.
                string file = ofd.SafeFileName;
                string Extension = Path.GetExtension(ofd.FileName);
                Import_To_Grid(ofd.FileName, Extension, isRDB);


        private void Import_To_Grid(string FilePath, string Extension, string isHDR)
            string conStr = "";
            switch (Extension)
                case ".xls": //Excel 97-03
                    conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
                case ".xlsx": //Excel 07
                    conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
            conStr = String.Format(conStr, FilePath, isHDR);
            OleDbConnection connExcel = new OleDbConnection(conStr);
            OleDbCommand cmdExcel = new OleDbCommand();
            OleDbDataAdapter oda = new OleDbDataAdapter();
            DataTable dt = new DataTable();
            cmdExcel.Connection = connExcel;

            //Get the name of First Sheet
            DataTable dtExcelSchema;
            dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();

            //Read Data from First Sheet
            cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
            oda.SelectCommand = cmdExcel;

            //Bind Data to GridView

            dataGridView1.DataSource = dt;

        string isRDB = string.Empty;
        private void rdbY_CheckedChanged(object sender, EventArgs e)
            isRDB = "Yes";

        private void rdbN_CheckedChanged(object sender, EventArgs e)
            isRDB = "No";

App.Config settings

    <add name="Excel03ConString" connectionstring="Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}; Extended Properties='Excel 8.0;HDR={1}'" />
    <add name="Excel07ConString" connectionstring="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties='Excel 8.0;HDR={1}'" />
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Right Click Datagrid View and choose Properties in that change Column Header Visible= False.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.180621.3 | Last Updated 27 Nov 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100