We help IT Professionals succeed at work.
Get Started

How do I get the top row of a excel spreadsheet to be treated as a header row.

Richard Comito
Last Modified: 2010-04-23
I have the following code that I am using to pull information from spreadsheet and then filling a DataTable with the results.  When I use the DataTable as the source for the DataGridView, I get all the data I need.  However the top row I would like to use as the header for the column names.  What is going on right now is that the first column is an integer with the name idEmail and the second column is a Char with the name fldEmail.  What I get on the FIRST row and first column in the DataGridView is a blank because that column is set for Integer.  is there a way I can recognize the top row as a header name for each column instead of tht F1, F2 that is there now?

This is what I have right now.


                    'Create String for excelFileName
                    Dim excelFileName As String = String.Empty
                    'Confirming that ofd has a value
                    If Len(Me.ofd.FileName) > 0 Then
                        excelFileName = ofd.FileName
                    End If
                    ' Clear object

                    'Confirm that a value has been past to excelFileName
                    If excelFileName.Length > 0 Then
                        'Create Connction String for Excel
                        Dim connection As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & excelFileName & ";" & "Extended Properties=""Excel 8.0;HDR=No""")
                        'Create an adapter for Excel
                        Dim adapter As New OleDb.OleDbDataAdapter("Select * from [Sheet1$]", connection)
                        Dim dt As New DataTable("Sheet1")


                        lblDGVCountShow.Text = dt.Rows.Count.ToString

                        dgvEmailList.DataSource = dt
Watch Question
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE