Solved

In ComboBox, Adding - Select Item on index -1

Posted on 2010-08-25
12
261 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
Industry Leaders: 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 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

Industry Leaders: 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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 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