VS 2008 How to add an item to the top of a combobox? Thanks.-VBForums
Results 1 to 6 of 6

Thread: How to add an item to the top of a combobox? Thanks.

  1. #1

    Thread Starter
    New Member mdivk's Avatar
    Join Date
    Jul 2014
    Posts
    8

    Question How to add an item to the top of a combobox? Thanks.

    Hello,

    I have a combobox which is populated using datatable:
    Code:
                    Dim dt As New DataTable
                    dt = GetPatientList()
    
                    cbEPCDPatient.DataSource = dt
                    cbEPCDPatient.DisplayMember = "Patient"
                    cbEPCDPatient.ValueMember = "ID"
    I want to add a new item manually as the first item in the combobox, here is what I added right after the above code:

    Code:
                    cbEPCDPatient.Items.Add(New DictionaryEntry("Select Patient", 0))
    It seems this item is NOT added to the combobox.

    What is the right way to make this?

    Thank you very much.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,156

    Re: How to add an item to the top of a combobox? Thanks.

    You would use the Insert method rather than the Add method:
    Code:
    MyComboBox.Items.Insert(0, "First Item")
    Edit - I just realized that your ComboBox is bound to a DataTable. If this is the case, then you would need to insert a row to your DataTable:
    Code:
    Dim r As DataRow = dt.NewRow()
    r(0) = "Hello World!"
    
    dt.Rows.InsertAt(r, 0)
    Last edited by dday9; Aug 12th, 2015 at 08:49 AM.

  3. #3

    Thread Starter
    New Member mdivk's Avatar
    Join Date
    Jul 2014
    Posts
    8

    Re: How to add an item to the top of a combobox? Thanks.

    Thank you for your quick reply:
    Code:
                    Dim dt As New DataTable
                    dt = GetPatientList()
    
                    cbEPCDPatient.DataSource = dt
                    cbEPCDPatient.DisplayMember = "Patient"
                    cbEPCDPatient.ValueMember = "ID"
    
                    'Dim r As DataRow = dt.NewRow()
                    'r(0) = "Select Patient"
    
                    'dt.Rows.InsertAt(r, 0)
    
                    cbEPCDPatient.Items.Add(New DictionaryEntry("Select Patient", 0))
    After added the code, the combobox shows no item at all, how do I fix it?

  4. #4

    Thread Starter
    New Member mdivk's Avatar
    Join Date
    Jul 2014
    Posts
    8

    Re: How to add an item to the top of a combobox? Thanks.

    After debugging, I believe the issue rests here:
    r(0) = "Select Patient"

  5. #5
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,375

    Re: How to add an item to the top of a combobox? Thanks.

    You've got it in the wrong order, do this instead;

    Code:
    Dim dt As New DataTable
    dt = GetPatientList()
    
    Dim r As DataRow = dt.NewRow()
    r(0) = "Select Patient"
    dt.Rows.InsertAt(r, 0)
    
    cbEPCDPatient.DataSource = dt
    cbEPCDPatient.DisplayMember = "Patient"
    cbEPCDPatient.ValueMember = "ID"
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  6. #6

    Thread Starter
    New Member mdivk's Avatar
    Join Date
    Jul 2014
    Posts
    8

    Re: How to add an item to the top of a combobox? Thanks.

    Fixed. Thank you.

    Code:
                    Dim dt As New DataTable
                    dt = GetPatientList()
    
                    Dim r As DataRow = dt.NewRow()
                    Try
                        r("ID") = 0
                        r("Patient") = "Select Patient"
                    Catch ex As Exception
                        Throw ex
                    End Try
    
    
                    dt.Rows.InsertAt(r, 0)
    
                    cbEPCDPatient.DataSource = dt
                    cbEPCDPatient.DisplayMember = "Patient"
                    cbEPCDPatient.ValueMember = "ID"

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.