【问题标题】:How to Fill Data From Database to datagridview and combobox - vb.net如何将数据从数据库填充到 datagridview 和组合框 - vb.net
【发布时间】:2014-06-05 19:51:51
【问题描述】:

我需要将数据库中的数据填充到我的 Datagrid 和两个 Combobox。

我有 3 张桌子,“Tipo”、“Marca”和“Modelo”。 表“Modelo”有两个外键来自“Tipo”和“Marca”。

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CarregarDados()
    End Sub

    Private Sub CarregarDados()
        cn.ConnectionString = "server=localhost;user id=root;database=automoveldb"
        cn.Open()
        'Load DataGridView
        Try
            With Cmd
                .CommandType = CommandType.Text
                .CommandText = "SELECT modelo.id Id, marca.Nome NomeMarca, tipo.Nome NomeTipo, modelo.Nome NomeModelo FROM modelo INNER JOIN Marca ON marca.id = modelo.IdMarca JOIN tipo ON tipo.id = modelo.IdTipo;"
                .Connection = cn
            End With
            MsgBox(Cmd.CommandText)
            With Da
                .SelectCommand = Cmd
                .Fill(dt)
                dgvModelo.DataSource = dt
            End With

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            cn.Close()
        End Try

        '*************************
        Try
            With Cmd
                .CommandType = CommandType.Text
                .CommandText = "SELECT * FROM Tipo;"
                .Connection = cn
            End With
            MsgBox(Cmd.CommandText)
            With Da
                .SelectCommand = Cmd
                .Fill(dt)
                cmbTipo.ValueMember = "Id"
                cmbTipo.DisplayMember = "Nome"
                cmbTipo.DataSource = dt
            End With

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            cn.Close()
        End Try
    End Sub

我的数据库

生成我的代码

我的组合框类型已填充,但在 datagridview 中添加了一个新列,我不想要它

【问题讨论】:

  • 您能解释一下您的问题吗?
  • 您遇到错误了吗?那里使用的所有数据库对象都只是突然出现而没有被实例化。你在用什么数据库
  • 我无法将数据加载到我的组合框,只有 datagridview,我将编辑等待
  • 查看'加载组合,我尝试这样做,但是,不起作用!
  • 如果我像这样执行我的 SQL ->“SELECT * FROM MODELO”,在 DatagridView 中,我的列将显示“id”、“idTipo”、“idMarca”、“Nome”,但我想要显示名字,为什么加入

标签: vb.net datagridview combobox


【解决方案1】:

我找到了解决这个问题的方法,所以如果有人想在同一个函数中填充数据网格和组合框,那就去做吧:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CarregarDados()
    End Sub

    Private Sub CarregarDados()
        cn.ConnectionString = "server=localhost;user id=root;database=automoveldb"
        cn.Open()
        '***********************Load DataGridView
        Try
            With Cmd
                .CommandType = CommandType.Text
                .CommandText = "SELECT modelo.id Id, marca.Nome NomeMarca, tipo.Nome NomeTipo, modelo.Nome NomeModelo FROM modelo INNER JOIN Marca ON marca.id = modelo.IdMarca JOIN tipo ON tipo.id = modelo.IdTipo;"
                .Connection = cn
            End With
            With Da
                .SelectCommand = Cmd
                .Fill(dt)
                dgvModelo.DataSource = dt
            End With

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            cn.Close()
        End Try
        '************************* Load ComboBox Tipo
        Try
            With Cmd
                .CommandType = CommandType.Text
                .CommandText = "SELECT * FROM Tipo;"
                .Connection = cn
            End With

            With Da
                .SelectCommand = Cmd
                .Fill(ds, "tipo")
            End With

            cmbTipo.ValueMember = "Id"
            cmbTipo.DisplayMember = "Nome"
            cmbTipo.DataSource = ds.Tables("tipo")


        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            cn.Close()
        End Try

        '************************* Load ComboBox Marca
        Try
            With Cmd
                .CommandType = CommandType.Text
                .CommandText = "SELECT * FROM Marca;"
                .Connection = cn
            End With

            With Da
                .SelectCommand = Cmd
                .Fill(ds, "marca")
            End With

            cmbMarca.ValueMember = "Id"
            cmbMarca.DisplayMember = "Nome"
            cmbMarca.DataSource = ds.Tables("marca")


        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            cn.Close()
        End Try
    End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多