We help IT Professionals succeed at work.

lstbox.selectedindex changed error when cbobox1.selectedvalue = lstbox1.selectedvalue

536 Views
Last Modified: 2013-11-26
I have been trying to figure this problem out for a while now. I'm new to programming and i keep getting stuck on this one issue and i cannot or do not know what i am looking for to figure it out.
I have a listbox that is bound to a dataset with multiple tables in the dataset. All objects on the form are bound to differant fields in the dataset except for one, a combobox.
Basically when i select an item in the listbox i want the selectedindex changed event to set the combobox selected value to a column in the listbox selected index but the listbox indexchanged event is still running and it gives an error. It will popup a messagebox with the correct value but the event is not finished firing so it doesn't know what value to assign to the combobox.

the code is
Private Sub lstUserName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstUserName.SelectedIndexChanged

Dim selectrow As DataRow

        Try
            If _loaded = 1 Then

                selectrow = Ds.User.Select("UserID = " & CInt(lstUserName.SelectedValue))(0)
                cbAccessLevel.SelectedValue = selectrow.Item("Column")  '--This is where it blows up

                MessageBox.Show(selectrow.Item("Column").ToString)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
            MsgBox(lstUser.SelectedIndex.ToString + " and " + lstUserName.SelectedIndex.ToString)
        End Try

    End Sub

Open in new window

Can someone point me to a good referance??  i think i am just not looking for the right stuff.
Comment
Watch Question

John (Yiannis) ToutountzoglouInstructor Multiengine Pilot

Commented:
Try This
Dim drv as datarowview=Ctype(me.lstUserName.SelectedItem,Datarowview)
dim SelLstName as integer =drv.Item("UserId")
.
.
.
.
cbAccessLevel.SelectedValue=Selstname

Open in new window

use cbAccessLevel.SelectedIndex instead

Author

Commented:
I have tried both previously. But the issue is the event has not completed which is trying to asssign a blank selectedvalue because the valuemember of the lstbox has been assigned until after the event is complete.
John (Yiannis) ToutountzoglouInstructor Multiengine Pilot

Commented:
Then you may try ...
If not isdbnull(Selstname) then ......

Author

Commented:
unfortunatly that didn't work either it is saying specifically that "Cannot set the SelectedValue in a ListControl with an empty ValueMember. Because the value members are empy until after the event is complete. I can call a messagebox giving the correct value from the column but the messagebox appears after the event is completeed which means all items in the listbox have been assigned a index and value. but how do i trick it or make it call another event which then allows the event to complete before trying to assigne the selected value......  sorry i'm not doing a very good job of explaining... i'm just not sure of all the terminology
Instructor Multiengine Pilot
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
that unfortunatly didn't work either. i am trying now to get the index of the dataset datatable row and then
DsD2Secure1.AppUser.Rows(selectrow).Item(0).ItemArray.GetValue(8)
to get the value of that column. but that isn't working working either
this is what i have got now and the commented out parts aren't working

If lstUserName.SelectedIndex > -1 Then
            Try
                'If Not IsDBNull(Selstname) Then
                Dim drv As DataRowView = CType(Me.lstUserName.SelectedItem, DataRowView)
                Dim SelLstName As Integer = drv.Item("V4Role")
                Dim dsIndex As Integer = 0
                If Not IsDBNull(SelLstName) Then
                    'cbAccessLevel.SelectedValue = SelLstName

                    selectrow = DsD2Secure1.AppUser.Select("UserID = " & CInt(lstUserName.SelectedValue))(0)
                    'DsD2Secure1.AppUser.Rows(selectrow).Item(0).ItemArray.GetValue(8)
                    dsIndex = DsD2Secure1.AppUser.Rows.IndexOf(selectrow)
                    'cbAccessLevel.SelectedValue = DsD2Secure1.AppUser.Rows(dsIndex).Item(8)
                    'cbAccessLevel.SelectedValue = selectrow.ItemArray(8)

                    'MessageBox.Show(selectrow.Item("V4Role").ToString)
                    MessageBox.Show(DsD2Secure1.AppUser.Rows(dsIndex).Item(8).ToString)
                End If

Open in new window

Author

Commented:
You are awesome! I finally got it!! Thank you for all your help in pointing me to look for the index at which the row is on!
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.