简体   繁体   中英

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

我相信问题在于您需要绑定表的DefaultView:

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

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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM