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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24069074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:39:56  来源:igfitidea点击:

How to Fill Data From Database to datagridview and combobox - vb.net

vb.netdatagridviewcombobox

提问by TMoraes

I need to Fill data from database to my Datagrid and two Combobox.

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

I have 3 tables, "Tipo", "Marca" and "Modelo". The table "Modelo" have two foreign key from "Tipo" and "Marca".

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

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

在此处输入图片说明

Result my code

结果我的代码

enter image description here

在此处输入图片说明

My combobox Type was filled but added in the datagridview a new column, i dont want it

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

采纳答案by TMoraes

I found a solution for that problem, so if somebody want fill a datagrid and combobox in a same Function, do it:

我找到了该问题的解决方案,因此如果有人想在同一个函数中填充数据网格和组合框,请执行以下操作:

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