Situation: need to read a CSV (txt) file that has no header and has 4 fields (all numeric), the number of lines will vary.
Sample data:
160827,00003,000000075,0000025
84144,00001,000000050,0000050
161060,00002,000000050,0000025
40015,00002,000000100,0000050
61955,00002,000000100,0000050
etc
etc
I want to read this file into a DataGridView for proofing and later insert all these lines into an Access db with 2 additional fields added.
I've been able to access, read/populate the CSV file into the Grid (code below) but I have the following problem(s):
If I set the 4 column headers and other parms of the Grid at design; my sub inserts 4 more columns when it reads/populates the Grid. OR
If I don't set the Headers and Parms, it reads the first line of the CSV into the Grid as Header info.
I tried setting the Gri'ds AutoGenerateColumns=false but that didn't help.
Can anyone point me in the right direction?
Thanks,
`Public Sub GetCsvData()
Dim csvFileFolder As String = "<filepath>"
Dim csvFileName As String = "CCPMIX201205.csv"
Dim connString As String = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" _
& csvFileFolder & ";Extended Properties=""Text;HDR=No;FMT=Delimited"""
Dim conn As New Odbc.OdbcConnection(connString)
'Open a data adapter, specifying the file name to load
Dim da As New Odbc.OdbcDataAdapter("SELECT * FROM [" & csvFileName & "]", conn)
'Then fill a data table, which can be bound to a grid
Dim dt As New DataTable
da.Fill(dt)
DataGridView1.DataSource = dt
With DataGridView1
'.AutoGenerateColumns = True
.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
End With
End Sub``