Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

In ComboBox, Adding - Select Item on index -1

Posted on 2010-08-25
12
Medium Priority
?
263 Views
Last Modified: 2013-11-27

VS 2005 / Vb.Net

 cmbState.DataSource = objDs.Tables(0)
        cmbState.Items.Insert(-1, "-- Select --")
        cmbState.DisplayMember = "StateShortName"
        cmbState.ValueMember = "StateID"
0
Comment
Question by:chokka
[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
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 10

Expert Comment

by:Jini Jose
ID: 33523177
add the ListItem "select" in the html section in design time.
then set the property AppendDataItems = true
0
 
LVL 10

Expert Comment

by:Jini Jose
ID: 33523187
sorry it is for web
0
 

Author Comment

by:chokka
ID: 33523229
ComboBox .. Its windows App .. ( VB.Net / VS 2005 )
0
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!

 
LVL 1

Expert Comment

by:narendragummadi
ID: 33523384
Do you want to insert "-- Select --" as frist entry in the combobox?
0
 

Author Comment

by:chokka
ID: 33523458
yes
0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33523479
-1 is not a valid index. Index is 0-based.
If you want to add "--Select--" as the first item in the combobox, you can do either of the following:

1. cmbState.Items.Insert(0, "-- Select --")
or
2. cmbState.Items.Add("-- Select --")
 before populating the records.

0
 

Author Comment

by:chokka
ID: 33523545


1. cmbState.Items.Insert(0, "-- Select --")
or
2. cmbState.Items.Add("-- Select --")
 before populating the records.


I tried both the ways .. But value is not reflecting in ComboBox.
Do i need to change some property settings in the ComboBox
0
 

Author Comment

by:chokka
ID: 33523554


Thats the actual Code

Public Sub PopulateDState()
        Dim objBL As New BusinessLayer.BusinessLayer
        Dim objDs As New DataSet
        objDs = objBL.PopulateState()

        'cmbDState.Items.Insert(0, "-- Select --")
        cmbDState.Items.Add("-- Select --")
        cmbDState.DataSource = objDs.Tables(0)

        cmbDState.DisplayMember = "StateShortName"
        cmbDState.ValueMember = "StateID"
    End Sub
0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33523598
Add the following line after the combo box is populated:

cmbDState.SelectedIndex = 0
0
 

Author Comment

by:chokka
ID: 33523626

I have added ... But still its not working ..!

I have kept the ComboBox inside a Panel .. was it a reason ?


    Public Sub PopulateState()
        Dim objBL As New BusinessLayer.BusinessLayer
        Dim objDs As New DataSet
        objDs = objBL.PopulateState()
        'cmbState.Items.Insert(0, "-- Select --")
        cmbState.Items.Add("-- Select --")
        cmbState.DataSource = objDs.Tables(0)

        cmbState.DisplayMember = "StateShortName"
        cmbState.ValueMember = "StateID"
        cmbState.SelectedIndex = 0
    End Sub
0
 
LVL 1

Accepted Solution

by:
narendragummadi earned 2000 total points
ID: 33523732
First add a new row to the dataset you are binding to the combobox and set values for the row and then insert that row at first postion.

Dim dr As DataRow = objDs.Tables(0).NewRow()
' create a new row into the dataset
dr(0) = "-- Select --"
dr(1) = 1102
.
.
.
objDs.Tables(0).Rows.InsertAt(dr, 0)

and now bind your dataset to the combo box
0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33524058
@narendragummadi's solution should work. You may want to check out this link:

http://www.codeproject.com/KB/combobox/addselecttoboundcombo.aspx

The code is in C#, but you can easily convert it to VB.NET here:
http://www.developerfusion.com/tools/convert/csharp-to-vb/

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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…
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

705 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