1

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".

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

7
  • Could you explain what is your problem? Commented Jun 5, 2014 at 19:55
  • are you getting an error? all the DB objects being used there just pop out of no where without being instanced. what DB are you using Commented Jun 5, 2014 at 19:56
  • I can't load data to my combobox, just datagridview, i'll edit wait Commented Jun 5, 2014 at 19:58
  • see 'load combo, is this way that i try to do, but, not work! Commented Jun 5, 2014 at 20:00
  • If i do my SQL like this -> "SELECT * FROM MODELO", In DatagridView, my columns will show "id", "idTipo", "idMarca", "Nome", but i want to show the name, why did Join Commented Jun 5, 2014 at 20:02

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.