0

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
4

2 回答 2

1

更改您的 Form3 类,添加几个全局变量并强制 Form3 的调用者传递初始化值。然后使用打开连接时传递的值

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

在这些更改之后,您的 Form1 需要打开 Form3 时会调用

   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()
于 2013-04-03T15:13:16.423 回答
0

我想在表单 1 上有一个打开 OpenFileDialog 的按钮,以便用户可以浏览 csv 文件。一旦用户选择文件,Form 3 上的 DataGridView 和 DataSet 就会启动

为了解决这个问题,你需要做两件事

1. 使用 OpenFileDialog

这很简单。只需使用OpenFileDialog改编自 MSDN 示例的以下片段打开文件对话框,如果

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.将fileLocation传递给Form3

形式只是对象。因此,您将信息从一个对象传递到另一个对象的所有方式都由您支配。

例如,您可以在 Form3 上创建一个公共属性FileLocation,然后在 Show Form3 之前设置它。此外,Form.Load您需要使用Form.Shown而不是使用。

另一种选择是重载具有 FileLocation 参数的 Form3 的构造函数。

于 2013-04-03T15:14:00.870 回答