Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel Dataset to Datagrid Column names

Status
Not open for further replies.

Skittle

ISP
Sep 10, 2002
1,528
US
I am reading an Excel file into a dataset and using the dataset to populate a datagrid.

The problem I have is that I need to be able to set the data grid column title names. Column names are appearing but I need to change them for display on the datagrid.
How can I assign column names within a dataset or modify the datagrid column names?

My Code:-

Dim m_sConn1 As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & OpenFileDialog1.FileName.ToString & _
";Extended Properties=""Excel 8.0;HDR=YES"""

'
' Define Connection To Excel
Dim ExcelConnection As New
System.Data.OleDb.OleDbConnection(m_sConn1)

'
' Define Command
Dim ExcelCommand As OleDbCommand = New OleDbCommand
ExcelCommand.CommandText = "SELECT * FROM [MySheet$]"

'
' Define DataAdapter
Dim ExcelDataAdapter As New OleDbDataAdapter
ExcelDataAdapter.SelectCommand = ExcelCommand
ExcelDataAdapter.SelectCommand.Connection =
ExcelConnection

'
' Define Dataset
Dim ExcelDataSet As New DataSet

'
' Get Dataset
ExcelConnection.Open()
ExcelDataAdapter.Fill(ExcelDataSet, "journals")
ExcelConnection.Close()

DataGrid1.DataSource = ExcelDataSet
DataGrid1.DataMember = "MySheet"


Dazed and confused
 
Since you've already created the grid in design mode, and identified the data source, than I believe you should be able to edit the Columns collection (a property of your DataGridView), and change the Headers there. It is probably possible to do it "on the fly" also, but I don't know the syntax for doing so.
 
I figured it out.
I changed the column names in the data set.

ExcelDataSet.Tables(0).Columns(0).ColumnName = "Type"



Dazed and confused
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top