Results 1 to 9 of 9

Thread: empty record in combo box

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2002
    Posts
    13

    empty record in combo box

    I need to add an empty record at the begining of a databound combo box.

    MTmace

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: empty record in combo box

    is it just to have the combobox be blank when the user gets to it?

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

    Re: empty record in combo box

    A data-bound control displays whatever its DataSource contains. If there is no blank entry in your DataSource then there is no blank entry in your ComboBox, period. This sounds like a hack anyway. As klienma intimated, I'm guessing that you don't actually want yuor user to select a blank entry, but rather you want to have the ability to selct no entry at all. To do this you need to set the SelectedIndex to -1. There is a bug that requires you to do this twice when the ComboBox is data-bound. If you want the user to be able to clear their selection, what I always do is to handle the KeyDown event for the ComboBox and test for the Delete key. When the user depresses the Delete key I set the SelectedIndex to -1 (twice).
    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

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2002
    Posts
    13

    Re: empty record in combo box

    I tried this and it did not work.

    With Me.cboBox
    .Items.Clear()
    .DataSource = dvView
    .DisplayMember = "field1"
    .ValueMember = "field1"
    .SelectedIndex = -1
    .SelectedIndex = -1
    End With

    Is this what you were talking about?

    MTmace

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

    Re: empty record in combo box

    It works for me. In fact, despite the fact that I've read that you have to set the SelectedIndex twice when your ComboBox is data-bound, the following code produced an empty ComoBox for me:
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim dt As New DataTable
    3.  
    4.         dt.Columns.Add("Col1", GetType(Integer))
    5.         dt.Columns.Add("Col2", GetType(String))
    6.  
    7.         For i As Integer = 1 To 10
    8.             Dim dr As DataRow = dt.NewRow()
    9.  
    10.             dr(0) = i
    11.             dr(1) = "Row " & i.ToString()
    12.             dt.Rows.Add(dr)
    13.         Next
    14.  
    15.         Dim dv As New DataView(dt)
    16.  
    17.         Me.ComboBox1.DataSource = dv
    18.         Me.ComboBox1.DisplayMember = "Col2"
    19.         Me.ComboBox1.ValueMember = "Col1"
    20.         Me.ComboBox1.SelectedIndex = -1
    21.     End Sub
    By the way, your calling Items.Clear is redundant because setting the DataSorce excludes any other items anyway. Also, it may be unnecessary to us a DataView as well, which I assume dvView is. All DataTables are associated with a DataView already through their DefaultView property. You would only have to specifically create a DataView if you wanted to be able to filter or sort it without affecting the DefaultView. Also, when you bind a DataTable to a control it is actually the contents of the DefaultView that is displayed. This means that if you filter or sort the DefaultView, the changes will be reflected in the control. All this means that you would usually just bind the DataTable itself to your control, except in very specific circumstances.
    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

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2002
    Posts
    13

    Re: empty record in combo box

    That is interesting. I copy and pasted your code into my form and the default value was Row1.

    I am using a dataview because I use the same datatable through out my form in other combo boxes. If I do not use dataviews then when I make a change to one combo box all the other values will change to what ever was last selected.

    MTmace

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

    Re: empty record in combo box

    Quote Originally Posted by MTmace
    That is interesting. I copy and pasted your code into my form and the default value was Row1.

    I am using a dataview because I use the same datatable through out my form in other combo boxes. If I do not use dataviews then when I make a change to one combo box all the other values will change to what ever was last selected.

    MTmace
    That sounds like legitimate use of DataViews. I just thought I'd mention it because I've seen many people use them unnecessarily. As for why the code I used works for me and not you, . I tested it in VS.NET 2003 on .NET 1.1 SP1.
    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

  8. #8

    Thread Starter
    New Member
    Join Date
    Jul 2002
    Posts
    13

    Re: empty record in combo box

    I am still having problems with this issue. I tried this with a new project without a problem. But still will not work with my main project.


    Weird

    More info:
    The combobox that I am having trouble with is in a tab control. If I move the combo box off of the tab control the above code does work.
    Why would the location of the combo box matter and how can I fix this problem?
    It is not that it is in a tab control that causes the problem. The combo box can not be in on the first tab.

    MTmace
    Last edited by MTmace; Dec 8th, 2005 at 06:29 PM.

  9. #9

    Thread Starter
    New Member
    Join Date
    Jul 2002
    Posts
    13

    Re: empty record in combo box

    This codes works:

    Me.TabControl1.SelectedTab = TabPage2

    Dim dt As New DataTable

    dt.Columns.Add("Col1", GetType(Integer))
    dt.Columns.Add("Col2", GetType(String))

    For i As Integer = 1 To 10
    Dim dr As DataRow = dt.NewRow()

    dr(0) = i
    dr(1) = "Row " & i.ToString()
    dt.Rows.Add(dr)
    Next

    Dim dv As New DataView(dt)

    Me.ComboBox1.DataSource = dv
    Me.ComboBox1.DisplayMember = "Col2"
    Me.ComboBox1.ValueMember = "Col1"
    Me.ComboBox1.SelectedIndex = -1



    I guess you need to have the page selected before you can access or modify certain methods.
    Another problem arises when I try to add

    Me.TabControl1.SelectedTab = TabPage1

    at the end of the code to have the initial page show.

    Is there a better way to handle this problem?

    MTmace

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