Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27514

OpenFileDialog imports csv into DataGridView and establishes DataSet

$
0
0
I am new to programming and I am using visual basic express 2010. This is my setup. I have Button1 on Form1. When selected, Form2 opens and displays values from a csv file. The values are displayed on a DataGridView and a DataSet is established so I can keep the values in memory. I was hoping to get some help in editing my code to use an openfiledialog instead of the coded path/filename.

So, Button1 on Form1 initiates OpenFileDialog. Form2 displays the DataGridView and establishes DataSet. My code follows:

Code:

Public Class Form2

    Private Sub Form2_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

        Dim file As String = "test.csv"
        Dim path As String = "C:\Users\laptop\Desktop\"
        Dim ds As New DataSet

        Try

            If IO.File.Exists(IO.Path.Combine(path, file)) Then
                Dim ConStr As String = _
                "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
                path & ";Extended Properties=""Text;HDR=No;FMT=Delimited\"""
                Dim conn As New OleDb.OleDbConnection(ConStr)
                Dim da As New OleDb.OleDbDataAdapter("Select * from " & _
                file, conn)
                da.Fill(ds, "TextFile")

            End If

        Catch ex As Exception
            MessageBox.Show(ex.ToString)

        End Try

        DataGridView1.DataSource = ds.Tables(0)

    End Sub
End Class


Viewing all articles
Browse latest Browse all 27514

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>