如何将数据从数据库填充到datagridview和combobox - vb.net

时间:2014-06-05 19:51:51

标签: vb.net datagridview combobox

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

我有3张桌子," Tipo"," Marca"和" Modelo"。 表" Modelo"有两个外键来自" Tipo"和"马卡"。

Print

 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

MyDatabase的

enter image description here

结果我的代码

enter image description here

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

1 个答案:

答案 0 :(得分: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