Let user add new items to a databound combobox
-
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.
-
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.
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 -
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