Combo and SelectedValue

G

Guest

Hi

I have a standard combo on a WinForm and use the following code to bind it
to a datatable (X returns a datatable object)

Dim X As New BusinessRules.Contracts.Agreements

With cmbAgreement
.DataSource = X.AgreementsByCustomer(66524)
.ValueMember = ("AgreementID")
.DisplayMember = ("Name")
End With

The list is populated correctly, however when the user selects a record I
want to retrieve the "AgreementID" of the "Name" selected.

The value of cmbAgreement.Text is what the user selected but
cmbAgreement.SelectedValue is always Nothing and .SelectedText is always " ".

How can I retrieve the "AgreementID" ?
 
G

Greg Burns

Your code looks correct to me. Not sure why you are doing this:
.ValueMember = ("AgreementID")
.DisplayMember = ("Name")

instead of simply this, but it seems to still work either way.
.ValueMember = "AgreementID"
.DisplayMember = "Name"

http://samples.gotdotnet.com/quickstart/winforms/doc/WinFormsData.aspx

Not sure what is going wrong with your code. Same sort of code seems to work
fine for me:

Private Sub ComboBox1_SelectionChangeCommitted(ByVal sender As Object,
ByVal e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted

Debug.Print(ComboBox1.SelectedText)
Debug.Print(ComboBox1.SelectedValue.ToString)

End Sub

Greg
 
G

Guest

Hi Greg

Thanks for the response, however removing the (), or putting code within the
SelectionChangeCommitted event has had any effect - I am completely baffled
!!!!

One thing I have noticed is within the SelectionChangeCommitted event the
Datasource property is now Nothing (??).

Would this have an effect ?

Stephen
 
G

Guest

Sorted !!!!!!!!!!

Need to do Ctype the value to an Integer (as AgreementID is an integer
value) so the following worked

MessageBox.Show("Selected value is " & CType(cmbAgreement.SelectedValue,
Integer))

:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top