Theveloper
Theveloper

Reputation: 806

VB.NET Bound ComboBox SelectedValue does not display

I bind a datatable to the combobox.DataSource on load. I then give the combobox a DisplayMember and a ValueMember (2 different columns out of the datatable). In the SelectedIndexChanged of the combobox I would like to use the SelectedValue property of the combobox, just to test I MsgBox(combobox.SelectedValue) and I get "Argument 'Prompt' cannot be converted to type 'String'." Why isn't it displaying the value? :(

OnLoad
    cbCISoftware.DataSource = dbMaps.Tables("maps")
    cbCISoftware.ValueMember = "id"
    cbCISoftware.DisplayMember = "name"

SelectedIndexChanged of cbCISoftware
    MsgBox(cbCISoftware.SelectedValue)

SelectedValue.ToString outputs
    System.Data.DataRowView

Upvotes: 0

Views: 5355

Answers (2)

Andrea Antonangeli
Andrea Antonangeli

Reputation: 1262

First of all you have to be sure to have selected DropDownList as DropDownStyle for the Combobox and that the binding is working.

Then you have to replace MsgBox(cbCISoftware.SelectedValue) with MsgBox(cbCISoftware.SelectedValue.ToString)

Otherwise to obtain the result, MsgBox(cbCISoftware.Text) will work, but it not probably what you are looking for :-)

I can provide you with complete code to do the binding if you need it.

Upvotes: 0

competent_tech
competent_tech

Reputation: 44921

I believe the issue is that you need to bind the table's DefaultView:

cbCISoftware.DataSource = dbMaps.Tables("maps").DefaultView

Upvotes: 1

Related Questions