Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Let user add new items to a databound combobox

Let user add new items to a databound combobox

Scheduled Pinned Locked Moved Visual Basic
help
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NDD57
    wrote on last edited by
    #1

    Wondered if anybody can help me with this. I want to have a databound combobox and the first item will be "Add New Item". The combobox will otherwise be populated from a table. If the user needs to enter a new item they can click on "Add New Item" and add a new record to the bound table and when the user returns to the combobox the new item will be selected (something similar to QuickBooks comboboxes). Any ideas or help much appreciated.

    S 1 Reply Last reply
    0
    • N NDD57

      Wondered if anybody can help me with this. I want to have a databound combobox and the first item will be "Add New Item". The combobox will otherwise be populated from a table. If the user needs to enter a new item they can click on "Add New Item" and add a new record to the bound table and when the user returns to the combobox the new item will be selected (something similar to QuickBooks comboboxes). Any ideas or help much appreciated.

      S Offline
      S Offline
      Scott Page
      wrote on last edited by
      #2

      I would suggest not binding to the combobox using the DataSource property as a solution. I'll give you a rough example, though I haven't tested it, I know it is possible. Assuming the following: A ComboBox named 'cboItems' A Button named 'btnUpdate' A TextBox named 'tbItemName' A Typed-DataSet named 'dsData' of type ExampleDataSet A Typed-DataTable in 'dsData' named 'ExampleItems' of type ExampleDataTable A DataColumn in 'ExampleItems' named 'ItemName' Public Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load 'We will implement custom sorting, so disable the ComboBox automatic sorting Me.cboItems.Sorted = False Me.RefreshItems End Sub Private Sub RefreshItems 'Create a temp StringCollection to sort our data Dim SortedItems As New Collections.StringCollection For Each IRow as ExampleDataSet.ExampleRow In Me.dsData.ExampleItems SortedItems.Add(IRow.ItemName) Next 'Sort the list before adding it to the ComboBox SortedItems.Sort Me.cboItems.Clear Me.cboItems.BeginUpdate 'Begin updating the combobox, recommended but not required all of the time 'Add the "Add New Item" first to ensure it is the first item Me.cboItems.Items.Add("Add New Item") For Each Name As String In SortedItems Me.cboItems.Items.Add(Name) Next Me.cboItems.EndUpdate 'Finished updating the combobox SortedItems = Nothing 'At this point, our ComboBox has at least one item "Add New Item" so we can safetly 'set the ComboBox.SelectedItem to 0 Me.cboItems.SelectedIndex = 0 'Alternative 'Me.cboItems.SelectedIndex = Me.cboItems.Items.IndexOf("Add New Item") End Sub Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAdd.Click Me.dsData.ExampleItems.AddExampleItemRow(Me.tbItemName.Text) 'After adding an item to the datasource, refresh the list to reflect the changes 'This can be done after deleting a row or updating it as well Me.RefreshItems End Sub The previous example will work. However, I would suggest possibly using a button on the main form that opens a child form when clicked. The child form would have all of the controls needed to add the item. When the child form closed you could then add an item. This would allow you to bind directly to the combobox and not worry about all of the previous code. Hope this helps, If you have any other questions feel free to ask Scott Page "Some people spend

      N 1 Reply Last reply
      0
      • S Scott Page

        I would suggest not binding to the combobox using the DataSource property as a solution. I'll give you a rough example, though I haven't tested it, I know it is possible. Assuming the following: A ComboBox named 'cboItems' A Button named 'btnUpdate' A TextBox named 'tbItemName' A Typed-DataSet named 'dsData' of type ExampleDataSet A Typed-DataTable in 'dsData' named 'ExampleItems' of type ExampleDataTable A DataColumn in 'ExampleItems' named 'ItemName' Public Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load 'We will implement custom sorting, so disable the ComboBox automatic sorting Me.cboItems.Sorted = False Me.RefreshItems End Sub Private Sub RefreshItems 'Create a temp StringCollection to sort our data Dim SortedItems As New Collections.StringCollection For Each IRow as ExampleDataSet.ExampleRow In Me.dsData.ExampleItems SortedItems.Add(IRow.ItemName) Next 'Sort the list before adding it to the ComboBox SortedItems.Sort Me.cboItems.Clear Me.cboItems.BeginUpdate 'Begin updating the combobox, recommended but not required all of the time 'Add the "Add New Item" first to ensure it is the first item Me.cboItems.Items.Add("Add New Item") For Each Name As String In SortedItems Me.cboItems.Items.Add(Name) Next Me.cboItems.EndUpdate 'Finished updating the combobox SortedItems = Nothing 'At this point, our ComboBox has at least one item "Add New Item" so we can safetly 'set the ComboBox.SelectedItem to 0 Me.cboItems.SelectedIndex = 0 'Alternative 'Me.cboItems.SelectedIndex = Me.cboItems.Items.IndexOf("Add New Item") End Sub Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAdd.Click Me.dsData.ExampleItems.AddExampleItemRow(Me.tbItemName.Text) 'After adding an item to the datasource, refresh the list to reflect the changes 'This can be done after deleting a row or updating it as well Me.RefreshItems End Sub The previous example will work. However, I would suggest possibly using a button on the main form that opens a child form when clicked. The child form would have all of the controls needed to add the item. When the child form closed you could then add an item. This would allow you to bind directly to the combobox and not worry about all of the previous code. Hope this helps, If you have any other questions feel free to ask Scott Page "Some people spend

        N Offline
        N Offline
        NDD57
        wrote on last edited by
        #3

        Thanks for all your help. I agree about that using a button may be a simpler option. I might actually use a context menu and let they user right click to bring up the form. Thanks again

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups