I am using Visual Basic Express 2010. Form 3 has code to import a csv file into a DataGridView while creating a DataSet. This event happens when I open Form 3. The path to the file is in the code. I would like to have a button on Form 1 that opens an OpenFileDialog so the user can browse for the csv file. Once the user selects the file, the DataGridView and DataSet on Form 3 initiate. The code I am currently using is below. Is there a way to edit the code to have an open file dialog from a button on Form 1 and not auto load through a pathway? Any assistance would be appreciated.
Public Class Form3
Private Sub Form3_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
End If
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=Yes;IMEX=1;FMT=CSVDelimited\"""
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
Change your Form3 class adding a couple of global vars and force the caller of Form3 to pass an initialization value. Then use the values passed in the opening of the connection
Public Class Form3
Dim mFileCSV As String
Dim mPathCSV As String
Public Sub New(ByVal fileCSV as String, ByVal pathCSV as String)
mFileCSV = fileCSV
mPathCSV = pathCSV
End Sub
Private Sub Form3_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Dim fullPath = Path.Combine(mPathCSV, mFileCSV)
if File.Exist(fullPath) then
.... open the connection ....
End Sub
after these changes your Form1 when need to open the Form3 will call
Dim of = new OpenFileDialog()
if of.ShowDialog() = DialogResult.OK Then
.... extract file and path and pass to form3 constructor
Dim f3 As Form3 = New Form3(file, path)
f3.Show()
I would like to have a button on Form 1 that opens an OpenFileDialog so the user can browse for the csv file. Once the user selects the file, the DataGridView and DataSet on Form 3 initiate
In order to solve this you need to do two things
1. Use an OpenFileDialog
This is easy. Just use the OpenFileDialog The below snippet adapted from the MSDN sample Opens a File Dialog box and if the
Dim openFileDialog1 As New OpenFileDialog()
Dim fileLocation As String
openFileDialog1.InitialDirectory = "c:\"
openFileDialog1.Filter = "txt files (*.csv)|*.csv|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 1
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
fileLocation = openFileDialog1.FileName
End If
2. Pass the fileLocation to Form3
Forms are just objects. So all the ways that you pass information from one object to another is at your disposal.
For example you could create a public property FileLocation
on Form3 and then set it before you Show Form3. Also instead of using Form.Load
you'd need to use Form.Shown
instead.
Another option would be to overload the Constructor for Form3 that has a FileLocation parameter.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.