|
-
Sep 28th, 2015, 05:39 PM
#1
Thread Starter
Junior Member
[RESOLVED] Customizing Command to pull data from Excel into DataGridView
I pulling data from an Excel sheet, which I know how to pull in all the data, but I only want to pull in certain columns. The column names are PROJECT NUMBER, REGION, COMPANY NAME, START DATE STATUS, ETC....... How can I write that into my command so I only pull in the columns I specify in code.
Code:
Private Sub cmbYear_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbYear.SelectedIndexChanged, rbAllProj.Click
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim DtSet As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
'thisthing = "C:\Users\Frank\Documents\Copy bbof FrankDocuments.xls"
MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + fileName + ";Extended Properties=Excel 12.0;")
'MyCommand = New System.Data.OleDb.OleDbDataAdapter("Select PROJECT NUMBER, REGION, COMPANY NAME, START DATE STATUS, STANDARD REPORT STATUS, DATA VIZUALIZATION STATUS from [" & cmbYear.Text & "$]", MyConnection)
MyCommand = New System.Data.OleDb.OleDbDataAdapter("Select PROJECT NUMBER from [" & cmbYear.Text & "$]", MyConnection)
'MyCommand.TableMappings.Add("A", "B")
DtSet = New System.Data.DataSet
'If DtSet.Tables.Count = 0 Then
' MessageBox.Show("Tab is Empty Stupid")
'Else
MyCommand.Fill(DtSet)
dgvProjects.DataSource = DtSet.Tables(0)
MyConnection.Close()
'End If
-
Sep 28th, 2015, 06:04 PM
#2
Thread Starter
Junior Member
Re: [RESOLVED] Customizing Command to pull data from Excel into DataGridView
Figured it out. SMH. Forgot the brackets around the columns.
Code:
MyCommand = New System.Data.OleDb.OleDbDataAdapter("Select [PROJECT NUMBER], [REGION] from [" & cmbYear.Text & "$]", MyConnection)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|