Read a CSV file into a datagridview in VB.NET

By Allen Stoner

Given a CSV file read it into a datagridview on a form.

    Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
            Dim odf As New FileInfo(OpenFileDialog1.FileName)
            Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Text;Data Source=" & odf.DirectoryName
            Dim objConn As New OleDbConnection(sConnectionString)
             objConn.Open()
            Dim objCmdSelect As New OleDbCommand("SELECT * FROM " & odf.Name, objConn)
             Dim objAdapter1 As New OleDbDataAdapter
            objAdapter1.SelectCommand = objCmdSelect
             Dim objDataset1 As New DataSet
            objAdapter1.Fill(objDataset1, "test")
            DataGridView1.DataSource = objDataset1.Tables(0).DefaultView
            objConn.Close()
        End If

Read a CSV file into a datagridview in VB.NET  (1911 Views)