Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
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...

Regards
Balamurugan
Posted
Comments
[no name] 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;\"");
theConnection.Open();
OleDbDataAdapter theDataAdapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", theConnection);
DataSet theDS = new DataSet();
dt = new DataTable();
theDataAdapter.Fill(dt);
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...

hi you can see this link http://www.ezzylearning.com/tutorial.aspx?tid=1553779[^]


just put the headings in your Excel file for each column


See one more link

http://sivanandareddyg.blogspot.in/2012/05/read-and-import-excel-sheet-into-aspnet.html[^]
 
Share this answer
 
v3
Comments
Balamurugan1989 27-Nov-12 1:35am    
I'm working in C# windows form application.
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

C#
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;
                    break;
                case ".xlsx": //Excel 07
                    conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
                    break;
            }
            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
            connExcel.Open();
            DataTable dtExcelSchema;
            dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
            connExcel.Close();

            //Read Data from First Sheet
            connExcel.Open();
            cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
            oda.SelectCommand = cmdExcel;
            oda.Fill(dt);
            connExcel.Close();

            //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

C#
<connectionstrings>
    <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}'" />
  </connectionstrings>
 
Share this answer
 
Right Click Datagrid View and choose Properties in that change Column Header Visible= False.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900