Solved

ComboBox SelectedIndexChanged ?

Posted on 2010-09-13
13
258 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 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