Link to home
Start Free TrialLog in
Avatar of Jess31
Jess31

asked on

ComboBox SelectedIndexChanged ?

vb.net / Winform.
When SelectedIndexChnage Event is called how can I test that there is a valid SelectedValue? Sometimes this event is called but the SelectedValue is {System.Data.DataRowView} (when debugging in Immediate window).
Avatar of nepaluz
nepaluz
Flag of United Kingdom of Great Britain and Northern Ireland image

can you post some code on how you populate the combobox?
ASKER CERTIFIED SOLUTION
Avatar of nepaluz
nepaluz
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
try this
Dim Drv As DatarowView =Ctype(Me.Combobox1.SelectedItem,DatarowView)
       Dim IntCombo as Integer = drv.Item("DataTableColumnName2")
       DIm StrCombo as String = drv.Item("DataTableColumnName2")
   

Open in new window

That is happening because when you bind a DafaultView of table or  a bindingSource to a combobox, you get the selected item as DataRowView..So to get tha selected Item you have to reffer exactly to the column you want to retrieve the Item ..

       Dim Drv As DatarowView =Ctype(Me.Combobox1.SelectedItem,DatarowView)
       Dim IntCombo as Integer = drv.Item("DataTableColumnName2")
       DIm StrCombo as String = drv.Item("DataTableColumnName2")

Open in new window

Avatar of Jess31
Jess31

ASKER

nepaluz,

here is how I am populating the combobox (see the method bellow)

Salesperson.FillSalespersonCombo(Me.AccountManagerIDComboBox)
Dim bs3 As New BindingSource(MyDataSet.Tables(0), Nothing)
Me.AccountManagerIDComboBox.DataBindings.Add("SelectedValue", bs3, "AccountManagerID")
Public Shared Sub FillSalespersonCombo(ByRef cbo As ComboBox)

        Dim tbl As String = "tblSalesperson"
        Dim key_col As String = "SalespersonID"
        '
        Dim sql As String = "Select SalespersonID, dbo.FullName(FirstName, LastName) As Salesperson From " + tbl + " Where Hidden=0 Order By Salesperson"
        Dim Conn As New SqlConnection(My.Settings.OrderEntrySalesConnectionString)

        Dim cboSalespersonDS As New DataSet
        Dim cboSalespersonAdapter As New SqlDataAdapter
        cboSalespersonAdapter.SelectCommand = New SqlCommand(sql, Conn)
        cboSalespersonAdapter.Fill(cboSalespersonDS, tbl)
        Dim cboCustomerDT As DataTable = cboSalespersonDS.Tables(0)

        cbo.DataSource = cboCustomerDT
        cbo.DisplayMember = "Salesperson"
        cbo.ValueMember = "SalespersonID"

    End Sub

Open in new window

Avatar of Jess31

ASKER

jtoutou:
I don't have trouble getting any of the elements, only to know when the control is not alive. When the control is not alive I don't want to try anything on it. But how to test to konw whether it is alive that is the question.
You mean when there is data in combobox?
if so check the Rows.Count ..if 0 then thereis nothing to your combo....
Avatar of Jess31

ASKER

itoutou
where is there a Rows.Count in ComboBox?
not in the combobox..from the data being populated in there..
For the combobox there is something common .....items.count
Avatar of Jess31

ASKER

This is event seems to triggered multiple times. The first time it is triggered I get an items.count = 14 but the .selectedvalue is:
{System.Data.DataRowView}
    System.Data.DataRowView: {System.Data.DataRowView}

In cases like these (when it's not completed initializing the CB) I am looking for a simple way to test and know that I can't get a value yet. Currently I'm using Try/End Try but I would like something cleaner.
you can use cboSalespersonDS.Tables(0).Rows.Count to make your chek
Avatar of Jess31

ASKER

Not quite - it is not live in this event sub, plus this might be populated (probably is, that is why I get a count of 14) but the ComboBox has not completed initializing.