Click here to Skip to main content
15,569,644 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using the below code to import data from excel file to datagrideview .

I want to import data from CSV file to datagrideview . getting error "Exter table is note in the expected format"

How to import csv data file to datagridview

What I have tried:

Private Sub ButBhav_Click(sender As Object, e As EventArgs) Handles ButBhav.Click
Dim cn As OleDbConnection
Dim cmd As OleDbCommand
Dim da As OleDbDataAdapter
Dim dt As DataTable
da = New OleDbDataAdapter
dt = New DataTable

OpenFileDialog1.Title = "Please select Save file"
OpenFileDialog1.InitialDirectory = "D:\Test"
'OpenFileDialog1.Filter = "Excel|*.xls|All Files|*.*"
OpenFileDialog1.Filter = "Excel|*.csv|All Files|*.*"
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Dim Filepath As String = OpenFileDialog1.FileName

cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Filepath & ";Extended Properties='Excel 8.0;IMEX=1;HDR=Yes;'")
cmd = New OleDbCommand("Select * from [abc$]", cn)
da.SelectCommand = cmd
da.Fill(dt)
DgvImport.DataSource = dt
End If
End Sub
Posted
Updated 22-Oct-22 2:26am
Comments
Ralf Meier 23-Oct-22 6:45am    
How does the data inside the CSV-File look like ... and how should it look inside the DGV ?

1 solution

You cannot import csv files as Excel formatted data. See Using OleDb to Import Text Files (tab, CSV, custom)[^] for the correct way to do it.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900