I need to Fill data from database to my Datagrid and two Combobox.
I have 3 tables, "Tipo", "Marca" and "Modelo". The table "Modelo" have two foreign key from "Tipo" and "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
MyDatabase
Result my code
My combobox Type was filled but added in the datagridview a new column, i dont want it
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.