Solved

ComboBox SelectedIndexChanged ?

Posted on 2010-09-13
13
255 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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
 

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
 

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
 

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
 

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
 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Not seen Link button 5 37
What can cause the styling on a .NET site to not be found? 17 25
Regular expression help 2 24
Sending receiving text messages in vb.net 15 32
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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