Results 1 to 9 of 9

Thread: How To : Blank Line at Top of Combobox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Location
    DFW
    Posts
    16

    How To : Blank Line at Top of Combobox

    I am using the following code to populate a combobox whose SelectedValue property is used to build part of an SQL string to do a lookup in a table.



    'Get Everything in a Dataset Object
    objData.SQL = "SELECT ConsignorID, ConsignorName FROM Consignors"
    objConsignorDS = New DataSet
    Call objData.FillDataSet(objConsignorDS, "Consignors")

    dtConsignors = objConsignorDS.Tables(0)

    'Bind the Consignors ComboBox
    cboConsignor.DataSource = objConsignorDS.Tables("Consignors")
    cboConsignor.DisplayMember = "ConsignorName"
    cboConsignor.ValueMember = "ConsignorID"


    The issue I have is that I am stuck with always having one Combobox item displayed, so the SQL statement cant be used currently to select "ALL" .

    I need to find out how to setup the combobox with an "ALL" as one of the selections given the method being used above. If I need to change it, then I will do that.

    Thanks in advance.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How To : Blank Line at Top of Combobox

    No, you're not always stuck with one of the selections showing. The answer to this question is the same as all the other times it's been asked: set the SelectedIndex to -1 and no item will be selected.

    When you bind a ComboBox to a data source the control will display all the items in the data source and only the items in the data source. If you want to add an extra item then you have two choices:

    1. Add the extra item to the data source.

    2. Don't bind the control, but rather add the items manually.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Location
    DFW
    Posts
    16

    Re: How To : Blank Line at Top of Combobox

    That is what I was afraid of.

    One question .... What if you manually added the "All" and then used a DataReader with a loop to complete the fill ?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How To : Blank Line at Top of Combobox

    I already addressed that:
    If you want to add an extra item then you have two choices:

    1. Add the extra item to the data source.

    2. Don't bind the control, but rather add the items manually.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Addicted Member
    Join Date
    Oct 2006
    Location
    Chennai, India
    Posts
    198

    Re: How To : Blank Line at Top of Combobox

    Adding the extra items to the Data source and setting the SelectedProperty to -1 would be the better option

    Thanks

  6. #6
    Junior Member
    Join Date
    Oct 2007
    Posts
    19

    Re: How To : Blank Line at Top of Combobox

    You probably need to change your sql query to the following query. This will do what you try to accomplish.

    objData.SQL = "SELECT '' AS ConsignorID, 'ALL' as ConsignorName UNION ALL SELECT ConsignorID, ConsignorName FROM Consignors"

  7. #7
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: How To : Blank Line at Top of Combobox

    sorry for jumping in here guys..
    I kind of have the same problem.

    Lets save my combobox is bound and textboxes in turn are bound to that when a selection is made.
    How can i add in like <Please select> which will keep my textboxes blank..
    I have tried the selected index but my textboxes still fill to the first item in the combobox
    if you catch my drift


    -----------------------------------------------------------

    Okay i have added a new record to the Datasource
    Problem is it appears at the bottom of the list
    lol
    keep you guys posted

    I am adding it to the datasource then im binding it
    Last edited by d2005; Dec 7th, 2007 at 04:53 AM.
    it works 60% of the time, all the time.

  8. #8
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: How To : Blank Line at Top of Combobox

    Hi guys,

    heres the latest.. little help needed

    i have changed the selected index and if selected index is -1 then clearing my textboxes ... works fine

    next part is clearing all again when they press delete
    works fine ( searched forums )
    but i also

    do not want them to be able to type into the combo box

    Select Case e.KeyCode
    Case Keys.Delete, Keys.Back
    Me.mycombo.SelectedIndex = -1
    'Case Else
    ' e.keycode = nothing - not allowing the user to type into my combo
    End Select

    any ideas greatly appreciated

    -----------------------------------------

    actually its probably best if i could say
    if its not on my list then clear
    Last edited by d2005; Dec 7th, 2007 at 06:51 AM.
    it works 60% of the time, all the time.

  9. #9
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: How To : Blank Line at Top of Combobox

    Quote Originally Posted by d2005
    actually its probably best if i could say
    if its not on my list then clear

    I am trying this on the leave event of the combo..
    it seems to always clear??

    Code:
         If Me.myCombo.Text > "" And Me.myCombo.SelectedText = "" Then
                MessageBox.Show("Please select a valid record")
                Me.myCombo.SelectedIndex = -1
                myCombo.Focus()
            End If
    it works 60% of the time, all the time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width