Solved

In ComboBox, Adding - Select Item on index -1

Posted on 2010-08-25
12
253 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now