Jump to content

Recommended Posts

Posted

Boas,

Tenho uma "pequena" dúvida:

- Pretendo preencher uma datagridview com dados provenientes de uma base de dados MySql, só que um dos campos tem de ser preenchido com valores provenientes de uma combobox. A datagridview tem de permitir inserir, alterar e eliminar os registos.

Existem 2 possiveis soluções.

1 - Preencher a datagridview com um dataAdapter :

        Dim da As MySqlDataAdapter
        Dim ds As DataSet
        Dim sqlQuery As String



        If conClientMS.State = ConnectionState.Closed Then
            conClientMS.Open()
        End If


        sqlQuery = "SELECT * FROM ENTIDADE_CONTACTO WHERE ID_ENTIDADE ='" & Me.ID_ENTIDADE.Text & "'"
        da = New MySqlDataAdapter(sqlQuery, conClientMS)
        ds = New DataSet

        da.Fill(ds, "myTable")

        Me.entDataGrid.DataSource = ds.Tables("myTable")

        conClientMS.Close()

Mas neste caso não sei como alterar o campo pretendido de textbox para combobox.

2 - Inserir as colunas que pretendo no datagridview, resolvendo o problema da combobox, mas como é que insiro os dados da base de dados nas celulas?!

        Dim myData As MySqlDataAdapter
        Dim myDataSet As DataSet
        Dim mySqlString As String
        Dim myDataReader As MySqlDataReader
        Dim mymCommand As New MySqlCommand


        If conClientMS.State = ConnectionState.Closed Then
            conClientMS.Open()
        End If


        mySqlString = "SELECT ID_CONTACTO, ID_ENTIDADE, NOME, TELEFONE, FAX, EMAIL, ESTADO FROM ENTIDADE_CONTACTO"
        mymCommand.CommandText = mySqlString
        mymCommand.Connection = conClientMS
        myDataReader = mymCommand.ExecuteReader


        While myDataReader.Read()
            Me.entDataGrid.Rows.Add()
            'Me.entDataGrid.Columns(0).CellTemplate.Value = myDataReader.Item("ID_ENTIDADE")
            'If Not IsDBNull(myDataReader.Item("ID_ENTIDADE")) Then Me.entDataGrid.Item(0 = myDataReader.Item("ID_ENTIDADE")

        End While

Estou a ficar um pouco confuso, porque já procurei vezes sem conta, uma solução na internet e não encontro.

Help ME!!!

Cumps

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.