Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 353
  • Last Modified:

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
0
YZlat
Asked:
YZlat
  • 3
  • 2
1 Solution
 
TorrwinCommented:
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now