按钮列在从mysql数据库填充datagridview时重复

时间:2016-10-20 08:17:43

标签: mysql vb.net datagridview

我创建的程序将从文件路径中打开特定文件。文件路径存储在mysql数据库中。我总共有6列。前五列来自mysql数据库,最后一列是按Form_Load方法硬编码的按钮列。当我不包括load_data子按钮时,按钮无法找到文件路径。

在第一次按下按钮时,文件会打开,但是当我按下另一个按钮时,按钮列会重复并给我一个错误。这是我的代码:

Private Sub load_data()

    DataGridView1.DataSource = Nothing
    DataGridView1.Rows.Clear()


    MySqlConn = New MySqlConnection
    MySqlConn.ConnectionString = "server=localhost;userid=root;password=password;database=ordinanceviewerdb"

    Dim SDA As New MySqlDataAdapter
    Dim dbDataset As New DataTable
    Dim bSource As New BindingSource

    Try
        MySqlConn.Open()
        Dim Query As String

        Query = "select * from ordinanceviewerdb.ordinancetbl"
        COMMAND = New MySqlCommand(Query, MySqlConn)
        SDA.SelectCommand = COMMAND
        SDA.Fill(dbDataset)
        bSource.DataSource = dbDataset
        DataGridView1.DataSource = bSource
        SDA.Update(dbDataset)

        MySqlConn.Close()

    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        MySqlConn.Dispose()
    End Try

    Me.DataGridView1.Columns("filepath").Visible = True

    Dim openbtn As DataGridViewButtonColumn = New DataGridViewButtonColumn
    openbtn = New DataGridViewButtonColumn()
    openbtn.HeaderText = "View"
    openbtn.Text = "Open File"
    openbtn.Name = "opnbtn"
    openbtn.UseColumnTextForButtonValue = True

    DataGridView1.Columns.Add(openbtn)

End Sub

Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick


    load_data()

    Dim filename As String = DataGridView1(e.ColumnIndex, e.RowIndex).Value.ToString()

    If e.ColumnIndex = 5 AndAlso File.Exists(filename) Then

        Process.Start(filename)

    End If
End Sub

0 个答案:

没有答案