Solved

ComboBox SelectedIndexChanged ?

Posted on 2010-09-13
13
259 Views
Last Modified: 2012-10-14
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).
0
Comment
Question by:Jess31
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
13 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 33667610
can you post some code on how you populate the combobox?
0
 
LVL 17

Accepted Solution

by:
nepaluz earned 500 total points
ID: 33667647
also, rather than usethe SelectedIndexChanged, try using the SelectedValueChanged
for example:

Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged

        Dim cb1 = DirectCast(sender, ComboBox)
        Dim scName = cb1.SelectedItem.ToString
        'then you can test the scName which is the value thats selected in the combo
    End Sub

Open in new window

0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33667904
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

0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33668108
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

0
 
LVL 1

Author Comment

by:Jess31
ID: 33673760
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

0
 
LVL 1

Author Comment

by:Jess31
ID: 33673775
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.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33673824
You mean when there is data in combobox?
if so check the Rows.Count ..if 0 then thereis nothing to your combo....
0
 
LVL 1

Author Comment

by:Jess31
ID: 33674123
itoutou
where is there a Rows.Count in ComboBox?
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33674158
not in the combobox..from the data being populated in there..
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33674173
For the combobox there is something common .....items.count
0
 
LVL 1

Author Comment

by:Jess31
ID: 33674322
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.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33674397
you can use cboSalespersonDS.Tables(0).Rows.Count to make your chek
0
 
LVL 1

Author Comment

by:Jess31
ID: 33674559
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question