Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ComboBox SelectedIndexChanged ?

Posted on 2010-09-13
13
Medium Priority
?
264 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 2000 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
Technology Partners: 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!

 
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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

972 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