Hey guys,

Im trying to include an item called "Please select ..." in a combobox which is populated using a dataadapter. As you can see below, combobox items are inserted into the item list using a tableadapter. It is followed by a set of codes that inserts "Please select ..." into the item list at index 0.
When I debug, lines of code after tableadapter.fill gets bypassed. Sigh....
Whats wrong with my codes?

Public Class Form2
 
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
       
        Me.Table1TableAdapter.Fill(Me.My_DataSet.Table1)
        Me.ComboBox1.Items.Insert(0, "Please select ...")
        Me.ComboBox1.SelectedItem = "Please select ..."
 
    End Sub
  
End Class

Recommended Answers

All 6 Replies

Is it possible to insert an item into a combobox that is data bound?

>>Is it possible to insert an item into a combobox that is data bound?
From what I know, not possible.

Otherwise, see if this helps.

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim x() As String = {"1", "2", "3"} '// Array for testing.
        ComboBox1.DataSource = x '// add as DataSource.
        reloadCmb(ComboBox1)
    End Sub

    Private Sub reloadCmb(ByVal selComboBox As ComboBox)
        With selComboBox
            '// store all items from cmb.
            Dim arlTemp As New ArrayList
            For Each itm As String In selComboBox.Items : arlTemp.Add(itm) : Next
            '// reload items after adding your item.
            .DataSource = Nothing '// clear the connection from cmb, should clear the cmb.Items.
            .Items.Add("Please select...") '// add your item.
            For Each itm As String In arlTemp : selComboBox.Items.Add(itm) : Next '// add DataSource items.
            .SelectedIndex = 0 '// select first item.
        End With
    End Sub

Hi,

If your ComboBox is data bound, you cannot insert an item by using the ComboBox.Items.Insert() method. You have to insert the item in the data source. And you don't need an event to insert the item, just insert the item to the data source right after you assigning DataSource value for the ComboBox.

I hope it helps.

Its not possible to insert an item into a databound comboBox. One way you can do the trick is this:
> Make the 'Please Select...' the first data in your table in your database
> Add additional fields in the database
> write your databaound codes to load items from a column in a table in the database to fill up the comboBox
> Code to validate the users selection from the comboBox

I need to learn db related stuff just not to look like a noob around some of you guys, although feeling like a noob is kind of nice since I am not feeling on top of the world all the time and brings me back to reality.

I wanted to do this without having ' Please select...' stored in my database. I tried the solution provided by codeorder but somehow after ' Please select...' nothing is populated into the item list except for ' Please select...'. Is there a way to do this by using SQL query? Like using SELECT statement.
This is the query that I am using to fill the combobox.

SELECT        WRID, ControlID, Issue, Status, Remark
FROM            tblWrongRA
WHERE        (Status = 'Confirmed') AND (WRID IN
                             (SELECT        WRID
                               FROM            tblWrongRADetail))
UNION
SELECT        WRID, ControlID, Issue, Status, Remark
FROM            tblWrongRA AS tblWrongRA_1
WHERE        (Status = 'Resolved') AND (WRID IN
                             (SELECT        WRID
                               FROM            tblWrongRADetail AS tblWrongRADetail_1
                               WHERE        (ResolvedDate > GETDATE() - 15)))

This is the query that I am trying to run. To me everything looks right but some how my query do not run.

SELECT       "","Please Select...","","",""
UNION
SELECT        WRID, ControlID, Issue, Status, Remark
FROM            tblWrongRA
WHERE        (Status = 'Confirmed') AND (WRID IN
                             (SELECT        WRID
                               FROM            tblWrongRADetail))
UNION
SELECT        WRID, ControlID, Issue, Status, Remark
FROM            tblWrongRA AS tblWrongRA_1
WHERE        (Status = 'Resolved') AND (WRID IN
                             (SELECT        WRID
                               FROM            tblWrongRADetail AS tblWrongRADetail_1
                               WHERE        (ResolvedDate > GETDATE() - 15)))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.