Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem getting valuemember from combobox

Status
Not open for further replies.

purplehaze1

Programmer
Jul 23, 2003
86
US
I have combobox that shows vendors in dropdown list where
vendorName is displaymember and vendorID is valuemember.
I want to get vendorID when user selects vendor from the
dropdown list, but currently I am not able to get it (code below).
I get the following error:

An unhandled exception of type 'System.ArgumentException' occurred in system.data.dll

Additional information: Cannot set Column 'vendor_id' to be null. Please use DBNull instead.


cbvendorType.selectedvalue shows nothing.The combobox is placed in datagrid.
How can I get valuemember for selected vendor? Thanks.


Private Sub PopulateVendor()


Dim dsVendor As DataSet
Dim oVendor As Vendor
Dim iRow As DataRow

oVendor = New Vendor()
dsVendor = New DataSet()
dsVendor = oVendor.getVendors()
With cbVendors
.Items.Clear()
For Each iRow In dsVendor.Tables("Vendor").Rows
.DisplayMember = iRow.Item("vendName")
.ValueMember = iRow.Item("vendID")
.Items.Add(iRow.Item("vendName"))
Next
End With
dsVendor = Nothing
oVendor = Nothing

End Sub



Private Sub cbvendors_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbvendors.SelectedIndexChanged
Dim currentrow As Integer = grdDetails.CurrentCell.RowNumber
Dim currentColumn As Integer = grdDetails.CurrentCell.ColumnNumber
grdDetails.Item(currentrow, currentColumn + 1) = cbvendors.SelectedValue
End Sub

 
test to see if cbvendors.SelectedValue equals anything. sounds like it doesn't and it's trying to put that in the grid, in which case, you have to use dbnull.value to put in the grid. try this:

if cbvendors.selectedvalue = nothing then
grdDetails.Item(currentrow, currentColumn + 1) = dbnull.value
else
grdDetails.Item(currentrow, currentColumn + 1) = cbvendors.SelectedValue
end if

 
that'll prevent from erroring out which will work.
But what I really want is to get value member for the selected vendor, and it always seems to be nothing.
 
and you've tested with the command window to see if selectedtext, selecteditem, and selectedindex return something (at least one should).
 
I tested with command window. Selectedindex returns the index value but not value member, selectedtext & selecteditem returns the display member value, but I wanted to get value member, any ways I can get it?
 
I think your not filling the combo right.

Code:
With cbVendors
            .Items.Clear()
            For Each iRow In dsVendor.Tables("Vendor").Rows
                .DisplayMember = iRow.Item("vendName")
                .ValueMember = iRow.Item("vendID")
                .Items.Add(iRow.Item("vendName"))
            Next
        End With

should be or can be

Code:
With cbVendors
 .Items.Clear()
 .datasource = dsVendor.Tables("Vendor")
 .DisplayMember = "vendName"
 .ValueMember = "vendID"
End With




Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
try that pruple, i wasn't sure if it worked the same with comboboxes in grids, but that is how you set the display and valuemembers for comboboxe controls on their own. if that does not work, then what you can do is use the display member or index to search the table for the value (only use this if nothing else works, because its a big pain to do it).

and just so i am sure, i hate to ask again, but the selectedvalue returns "nothing" or null right? (just want to make sure it doesn't show system.data.row or something similar)
 
Hayt,
yes, the selectedvalue return "nothing".
Chrissie's way works, thanks guys.
Working it that way presents one problem.
I have this vendor combobox placed in datagrid.
When user selects vendor, I got to display vendor address.
So on event selected index changed, I have code to display address. However, selectedindex changed event gets fired when datasource is assigned to combo-box like Chrisse mentioned. That is why I did it the other way, but couldn't get value member i.e.

With cbVendors
.Items.Clear()
.datasource = dsVendor.Tables("Vendor")
.DisplayMember = "vendName"
.ValueMember = "vendID"
End With

So, the function to get address is fired when loading the vendors in combobox. Any suggestions to get around this problemo? Thanks again.

Private Sub cbVendors_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbVendors.SelectedIndexChanged
FindVendorAddresses()
End Sub
 
one thing you can do, is to set a default value for the combobox. if the combobox is dropdownlist, you can set the selectedindex to -1, this should set it to be blank, otherwise, if it is a dropdown, you can set the selectedtext to be " ". this will fire the index_changed, but it should set the address field to blank also. this might be a way to get around it.

i am not entirely sure what you are saying is the issue. anytime you have a combobox chage event function, it will fire on load of form, unless you set the values in the properties section of the form designer.

if these don't work for you, you might want to create a dummy row, perhaps filled with astericks, and set the sort on your dataset datatable to begin with the dummyrow.

let me know if this helps. also, try this website, it's got some great stuff in it.


unfortunately, it sometimes seems as though microsoft could have made comboboxes better in .net (especially when in grids).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top