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

x
?
Solved

In ComboBox, Adding - Select Item on index -1

Posted on 2010-08-25
12
Medium Priority
?
264 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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

772 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