Clear/Populate combo box with cbo.SelectedValue instead of cbo.Text
Posted on 2006-05-18
Do you have to clear/populate a combo box in some other way than setting SelectedValue? I am currently receiving the same error in two places when I attempt to set SelectedValue on a combo box programatically instead of populating it by clicking on it. I can set it with cmbo.Text = x ... only that requires me to modify several stored procedures to include outer joins of other tables to get description fields. I would prefer not to do that.
The error is 'Specified argument was out of the range of valid values'.
It first occurs when I press the Clear/New button to clear the fields using ClearFields() function. It is here that I am attempting to set SelectedValue = 0. I have a Public function that inserts a row into the datatable before initializing the dataview for the combo box.
It occurs again if I click/update a PropertiesOwned combo box that when updated also updates the cmboSubdivision combo box with
' cmboSubdivision.SelectedValue = drv("subdivision.id").ToString(). Here again, it will work properly if I populate it with the description using the Text property.
Below is my code:
dtSubdivision = GetSubdivisionDS().Tables("tbl_subdivision")
dvSubdivision = dtSubdivision.DefaultView
.DataSource = dvSubdivision
.ValueMember = "subdivision_id"
.DisplayMember = "subdivision_description"
Private Sub ClearFields()
cmboContact.SelectedValue = 0
cmboSubdivision.SelectedValue = 0
cmboPropertiesOwned.SelectedValue = 0
cmboStreet.SelectedValue = 0
Private Sub cmboPropertiesOwned_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmboProperty.SelectedIndexChanged
If cmboPropertiesOwned.SelectedIndex <> 0 Then
Dim drv As DataRowView = dvProperties(cmboPropertiesOwned.SelectedIndex)
cmboSubdivision.SelectedValue = drv("subdivision_id").ToString
cmboContact.SelectedValue = drv("contact_id").ToString
cmboStreet.SelectedValue = drv("property_id").ToString
Is it improper to attempt to clear a combo box using SelectedValue or populate a combo box with SelectedValue even though you have ValueMember set in the initialization of combo box properties?