Question Error with 2 related Combobox

ChinnuBlr

Member
Joined
Sep 30, 2009
Messages
10
Programming Experience
Beginner
Hi,

I am facing prblm in selecting compbobox...

I have a 2 combobox...cboxSelunits and cboxseldept

In selected index change of Cbxunit i am filling CbxDept.....

and in selected index change of cboxseldept i need to populate the treeview

-----------------------------------------------------------------------

VB.NET:
Private Sub cboxSelunits_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboxSelunits.SelectedIndexChanged
        Try
           
            If con.State = ConnectionState.Open Then
                con.Close()
            End If
            con.Open()
            _da1 = New SqlDataAdapter("select departmentname from department where unitid='" & unitid & "'  ", con)
            _dt = New DataTable
            _da1.Fill(_dt)
            With cboxseldept
                .DataSource = _dt
                .DisplayMember = "departmentname"
                .SelectedIndex = -1
            End With
            cboxseldept.Visible = True
            lbldept.Visible = True
            con.Close()
        Catch ex As Exception
            con.Close()
        Finally
            con.Close()
        End Try
    End Sub

------------------------------------------------------------------------


 Private Sub cboxseldept_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboxseldept.SelectedIndexChanged
                  
 PopulateTree()
            
End Sub


--------------------------------------------------

But error i am facing is when i am selecting cboxselUnits ifself cboxseldept is getting filled as well as treeview is getting populated......
 
Last edited by a moderator:
When you bind data to ComboBox, the first item in the data source is selected by default and there's nothing you can do about that. You can unselect the item afterwards but you can't stop it being selected in the first place. As such, you'll need code to ensure you ignore that initial selection, e.g.
VB.NET:
Private ignoreSelection As Boolean = False

Private Sub ComboBox1_SelectedIndexChanged() Handles ComboBox1.SelectedIndexChanged
    Me.ignoreSelection = True

    'Bind other ComboBox here.

    Me.ComboBox2.SelectedItem = Nothing
    Me.ignoreSelection = False
End Sub

Private Sub ComboBox2_SelectedIndexChanged() Handles ComboBox2.SelectedIndexChanged
    If Me.ignoreSelection Then
        Me.ClearTree()
    Else
        Me.PopulateTree()
    End If
End Sub
 
Hi,

Thanks a lot.... It worked for me.....

It was a great help from you.... :):D


When you bind data to ComboBox, the first item in the data source is selected by default and there's nothing you can do about that. You can unselect the item afterwards but you can't stop it being selected in the first place. As such, you'll need code to ensure you ignore that initial selection, e.g.
VB.NET:
Private ignoreSelection As Boolean = False

Private Sub ComboBox1_SelectedIndexChanged() Handles ComboBox1.SelectedIndexChanged
    Me.ignoreSelection = True

    'Bind other ComboBox here.

    Me.ComboBox2.SelectedItem = Nothing
    Me.ignoreSelection = False
End Sub

Private Sub ComboBox2_SelectedIndexChanged() Handles ComboBox2.SelectedIndexChanged
    If Me.ignoreSelection Then
        Me.ClearTree()
    Else
        Me.PopulateTree()
    End If
End Sub
 
Back
Top