Click here to Skip to main content
11,488,212 members (75,451 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0
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 26-Nov-12 18:59pm
Comments
Krunal R at 27-Nov-12 0:36am
   
Show your code..
Balamurugan1989 at 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;
mahesh07 from Pune at 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
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
v3
Comments
Balamurugan1989 at 27-Nov-12 1:35am
   
I'm working in C# windows form application.
Rate this: bad
good
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;
                    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

<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>
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 489
1 OriginalGriff 328
2 Mehdi Gholam 145
3 Sascha Lefèvre 135
4 KarstenK 115
0 Sergey Alexandrovich Kryukov 9,308
1 OriginalGriff 8,352
2 Sascha Lefèvre 3,334
3 Maciej Los 2,956
4 Richard Deeming 2,370


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

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