ComboBox question

I have a ComboBox that gets filled from the database:

cbTest.DataSource = ds.Tables(0).DefaultView

cbTest.DisplayMember = "name"
cbTest.ValueMember = "ID"

When form is loaded, the first customer returned by the query is displayed in the combo box.
I want to display a text "Select" instead.

I tried cbText.Items.Add("Select")
and cbText.Items.Insert(0,"Select") but nothing works
LVL 35
YZlatAsked:
Who is Participating?
 
TorrwinConnect With a Mentor Commented:
You could add a row to your dataset something like:

        Dim NewDR As DataRow
        NewDR = dsTemp.Tables(0).NewRow
        NewDR(2) = "Select..."                                    <-----2 is for column 2
        dsTemp.Tables(0).Rows.Add(NewDR)

        'Bind the data
        ddlCompName.DataBind()

        'Initially show blank row in ComboBox.
        ddlCompName.SelectedIndex = (dsTemp.Tables(0).Rows.Count) - 1

This is what i use for my web application, but the syntax wouldn't be much different.

I think you would use: cbText.SelectedText = "Select..."
0
 
YZlatAuthor Commented:
Torrwin , I am using VB.NET not ASP.NET

In VB.NET there is no DataBind method
0
 
YZlatAuthor Commented:
also
 NewDR(2) = "Select..."                                    

should be

 NewDR(1) = "Select..."        

and I get invalid index error here:
   'Initially show blank row in ComboBox.
        ddlCompName.SelectedIndex = (dsTemp.Tables(0).Rows.Count) - 1
                           
0
 
YZlatAuthor Commented:
Ok, I've changed your code around a little and it worked:

 Dim NewDR As DataRow
        NewDR = ds.Tables(0).NewRow
        NewDR(1) = "Select One"
        ds.Tables(0).Rows.InsertAt(NewDR, 0)

        If ds.Tables(0).Rows.Count > 0 Then
            cbTest.DataSource = ds.Tables(0).DefaultView
        End If
        cbTest.DisplayMember = "name"
        cbTest.ValueMember = "ID"
0
 
TorrwinCommented:
I said that my code was for a web application so the syntax for you would be a little different. =P
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.