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).
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).
can you post some code on how you populate the combobox?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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")
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")
ASKER
nepaluz,
here is how I am populating the combobox (see the method bellow)
Salesperson.FillSalesperso nCombo(Me. AccountMan agerIDComb oBox)
Dim bs3 As New BindingSource(MyDataSet.Ta bles(0), Nothing)
Me.AccountManagerIDComboBo x.DataBind ings.Add(" SelectedVa lue", bs3, "AccountManagerID")
here is how I am populating the combobox (see the method bellow)
Salesperson.FillSalesperso
Dim bs3 As New BindingSource(MyDataSet.Ta
Me.AccountManagerIDComboBo
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
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.
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....
if so check the Rows.Count ..if 0 then thereis nothing to your combo....
ASKER
itoutou
where is there a Rows.Count in ComboBox?
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
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.
{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.Coun t to make your chek
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.