0

好的,所以我尝试使用数据网格视图显示我的 MYSQL 数据库中的所有内容。但我得到的是不工作,有人可以帮助我的代码

    Public Sub dataview()
    DataGridView1.DataSource = ""
    Dim bindingSource1 As New BindingSource()
    Try
    Dim cmd As New MySqlCommand("SELECT * FROM upload", db_con)

    With Me.DataGridView1
        .AutoGenerateColumns = True

        bindingSource1.DataSource = cmd
        .DataSource = bindingSource1

        .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
        .BorderStyle = BorderStyle.Fixed3D
        .EditMode = DataGridViewEditMode.EditOnEnter
    End With
        DataGridView1.DataSource = bindingSource1
    Catch ex As Exception
        MessageBox.Show("something went wrong")
    End Try
4

2 回答 2

0
Public Sub dataview()
DataGridView1.DataSource = ""
Dim bindingSource1 As New BindingSource()
Try
Dim cmd As New MySqlCommand("SELECT * FROM upload", db_con)

With Me.DataGridView1
    .AutoGenerateColumns = True

    bindingSource1.DataSource = cmd
    .DataSource = bindingSource1

    .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
    .BorderStyle = BorderStyle.Fixed3D
    .EditMode = DataGridViewEditMode.EditOnEnter
End With
    DataGridView1.DataSource = bindingSource1
Catch ex As Exception
    MessageBox.Show("something went wrong")
End Try

试试这个^^

于 2014-10-17T02:51:52.183 回答
0

您不能简单地将 MySqlCommand 传递给 BindingSource。
您应该使用 MySqlDataAdapter 类型的对象来获取命令并填充 DataSet/DataTable。该 DataTable/DataSet 应该用作您的数据源bindingSource1

Dim cmd As New MySqlCommand("SELECT * FROM upload", db_con)
Dim da = New MySqlDataAdapter (cmd)
Dim dt = New DataTable()
da.Fill(dt)
bindingSource1.DataSource = dt ' here assign the DataTable'

With Me.DataGridView1
    .AutoGenerateColumns = True
    .DataSource = bindingSource1
    .....
End With
于 2013-04-15T21:14:18.483 回答