Solved

In ComboBox, Adding - Select Item on index -1

Posted on 2010-08-25
12
258 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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 500 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

735 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