Solved

ComboBox SelectedIndexChanged ?

Posted on 2010-09-13
13
253 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now