Jump to content
  • Revista PROGRAMAR: Já está disponível a edição #60 da revista programar. Faz já o download aqui!

Sign in to follow this  
Baia

Coluna do datatable numa combobox

Recommended Posts

Baia

Algém me pode dizer como eu mostro uma coluna  do datatable numa combobox?

Ou seja... tenho um Datatable com varias colunas e queria colocar as linhas de uma coluna numa Combobox para selecção...  🤔 :P

Share this post


Link to post
Share on other sites
ByMySlf

Tens que atribuir um datasource à combobox. Neste caso será o DataTable que tens.

Posteriormente defines o ValueMember e o DisplayMember. Ou seja, atribuis ao ValueMember e ao DisplayMember as colunas que pretendes.

Exemplo

Share this post


Link to post
Share on other sites
bioshock

Dim connString as String = "mete aqui o caminho da bd;"
Dim connection As New SqlConnection(connString)
Dim query As String = "SELECT * FROM Tabela"
Dim da As New SqlDataAdapter(query, connection)
Dim dt As New DataTable
dt.Fill(ds, "Tabela")
With ComboBox1
    .DataSource = dt
    .DisplayMember = "Tabela_nome"
    .ValueMember = "Tabela_id"
    .SelectedIndex = 0
End With

Creio que seja isto, experimenta.

Share this post


Link to post
Share on other sites
ByMySlf

Dim connString as String = "mete aqui o caminho da bd;"
Dim connection As New SqlConnection(connString)
Dim query As String = "SELECT * FROM Tabela"
Dim da As New SqlDataAdapter(query, connection)
Dim dt As New DataTable
dt.Fill(ds, "Tabela")
With ComboBox1
    .DataSource = dt
    .DisplayMember = "Tabela_nome"
    .ValueMember = "Tabela_id"
    .SelectedIndex = 0
End With

Creio que seja isto, experimenta.

Só uma correcção:

da.Fill(dt)

ao invés de...

dt.Fill(ds, "Tabela")

Share this post


Link to post
Share on other sites

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
Sign in to follow this  

×

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.