Click here to Skip to main content
15,878,970 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
DgvImport.DataSource = dt
End If
End Sub
Updated 22-Oct-22 1:26am
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