Performing an action when a combobox value is selected - vb.net

I'm having some trouble doing something that seems very simple but I just don't know what I'm doing wrong....
On Form_Load I populate a combobox with
Datasource
DisplayMember
ValueMember
Here I set .selectedindex = - 1 so that nothing is selected. The combobox gets populated properly and everything is bells and whistles.
Now when user selects something from the drop down, I want to populate a DataGrid with a bunch of info based off the VALUE selected. So I pass and ID into the function that populates the DataGrid.
I'm trying to do this like this...
Private Sub cbo1_SelectedValueChanged(sender As Object, e As EventArgs) Handles cbo1.SelectedValueChanged
dim productID as string=""
cbo1.SelectedValue = ProductID
Call PopulateProductGrid(ProductID)
End Sub
I also have tryined with SelectedIndexChanged, but for both event I get an error
Cannot set the SelectedValue in a ListControl with an empty
ValueMember.

I'll wager that the issue is that you're binding in the wrong order. You should always set the DataSource last, not first. Try changing that and see if your issue goes away. The problem is that setting the DataSource means that the control is bound, so an item will be selected and the appropriate events raised before you have set the DisplayMember and ValueMember.

Related

VB.Net - Display Yes/No in a Combobox but have a value of 1/0

This one is really kicking my butt, i'm hoping someone can point me in the right direction.
I have a combobox on my form that i need to display a "yes" and a "no" instead of a "0" and "1" BUT to process a "0" and "1" depending on the selection. Make sense?
I have the 0 and 1 sitting in the Data > Items > Collection properties of the combobox.
I have tried playing around with the DisplayMember property and the ValueMember property but i cant seem to get these working.
Any ideas would be much appreciated!
In your Data > Items > Collection properties of the combobox
Delete every line in there and add the following, (in the same order)
NO
YES
Now double click on your combobox in the form in design mode.
In the event subroutine for the combobox - selected index changed event, add the following:
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
UserChoseReloadSurvey = ComboBox1.SelectedIndex
End Sub
And that will save either a 1 or 0 in a variable named UserChoseReloadSurvey.
Taking it for granted that you have an integer variable named UserChoseReloadSurvey

Binding combobox selected value to a specific column of current row (not a datagridview)

I have a database that I am using in a VB 2010 project. What I did was removed a textbox for a database field that I dragged onto the form and replaced it with a combobox. The field name was orderstatus.
The problem is this: since I removed the textbox field from the form, I can't seem to bind the combobox value to the field in the active record. In other words, orderstatus textbox doesn't exist anymore. I want to let the user pick a status from the combobox and store that value to orderstatus so that it's saved to the current record my database.
I want to do something like this:
Private Sub Button16_Click_1(sender As System.Object, e As System.EventArgs) Handles Button16.Click
orderstatus = ComboBox13.SelectedValue
Me.OrdersDataSet.orders(0).orderstatus = orderstatus
Me.Validate()
Me.OrdersBindingSource.EndEdit()
Me.TableAdapterManager12.UpdateAll(Me.OrdersDataSet)
End Sub
but it doesn't like my second line where I try to assign the value to the field, saying there is no row 0. All I want to do is put the selected value of the combobox into the orderstatus field of the record being created (or updated).
I've also tried using:
Me.OrdersDataSet.orders.orderstatusColumn = orderstatus
and I get a message saying that the property of the column is ReadOnly. I'm not sure how that's possible because I configured the dataset to update, etc.
I should probably mention that I'm not using a datagridview but a details view, if that makes a difference. I've seen some talk about how to do this using datagridview and don't know if that would work in my case.
What am I doing wrong? What should I use to update just the column I want in the current row?
Ugh, turns out I had forgot to set the databinding properties of the field in question to save directly to the binding source and also select the selecteditem to bind. It was right there and I didn't realize. Ya live and learn. :)

Make a button have multiple uses

okay... How do I explain this without being totally confusing?... Alright, I have this form that has MenuScripts (top-levels and second-levels). The problem that I am having is one of the second-levels is "Add" which brings you to another form when clicked. This other form has a button ("Record") and text boxes. This other form allows the user to input data and when the record button is clicked, the inputted data is written into a text file. Ok, so back to the first form. Another second-level MenuScript is "Update" which also brings the user to the other form; but first, the user has to click an item within a listbox to proceed. How do I get the data from the selected item to appear in the appropriate textboxes and how do I get the record button to update data instead of being confused and thinking it is only a add-data button?
Is there a way to use an "if" statement to say something like "if mnuAdd is clicked then" "elseif mnuUpdate is clicked then". Would something like that work for giving the record button multiple uses?
Also, if someone can give me some pointers on making sure the user selects an item within the listbox would definitely be a plus! Thanks, guys!
Unfortunately, I cannot add images since my reputation is too low.
Here is a visual representation of my ultimate goal
Easiest way: before displaying the second form set it's Tag property to something distinct – say "Add" or "Update" – depending on which menu item is selected. Then you just test the Tag value in the button's Click event and proceed accordingly.
As for determining whether a list item is selected: well if there isn't the ListBox's SelectedIndex property will be set to -1.
You need to put a public property on the second form (Details) which specifies which mode it is in. For instance, you could create a mode enumeration like this:
Public Enum EntryModes
AddBook
UpdateBook
End Enum
Then, define a public mode property on the second form, like this:
Public Property EntryMode As EntryModes
Get
Return _entryMode
End Get
Set(ByVal value As EntryMode)
_entryMode = value
End Set
End Property
Private _entryMode As EntryMode
Then, when you show the second form from the menu, just set the property first, before showing it:
Private Sub mnuAdd_Click(sender As Object, e As EventArgs)
Dim dialog As New DetailsDialog()
dialog.EntryMode = EntryModes.AddBook
dialog.ShowDialog()
End Sub
Private Sub mnuUpdate_Click(sender As Object, e As EventArgs)
Dim dialog As New DetailsDialog()
dialog.EntryMode = EntryModes.UpdateBook
dialog.BookToUpdate = ListBox1.SelectedItem
dialog.ShowDialog()
End Sub
As you can see, in the Upate menu click, I also added a line that passes the information for which book should be updated.

WinForms binding

I have some controls bound to a BindingSource control.
I want to do a calculation when the value changes in one control and set the result on another control.
Do I update the textbox the property is bound to or do I update the underlying entity which would update the control anyway (I hope)?
When I change combobox A (OnPropertyChange) textbox B is updated with the new calculated result. This works fine, but I have noticed that when I leave combobox A it reverts back to its original value. What is going on here!
Private Sub ComboBoxEditCostCode_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxEditCostCode.EditValueChanged
Select Case ComboBoxEditCostCode.EditValue
Case "7"
CType(TransactionEntityBindingSource.Current, TblTransactionsEntity).Qbdescription = "7-is here"
Case "2"
CType(TransactionEntityBindingSource.Current, TblTransactionsEntity).Qbdescription = "2-is here"
Case Else
CType(TransactionEntityBindingSource.Current, TblTransactionsEntity).Qbdescription = "7-is here"
End Select
End Sub
if we bind a control to a source, then if the source changes, we can make the its value automatically reflected in the control. About the problem you are facing, it would be better if you show the code snippet.
Tell more about your changing, how second text box is bound?
You have to change your initial data instead of changing textbox b value.
Also when textbox A loses it focus raises EndEdit event and I think binding mechanism refreshes value in textbox B.
You can control on which action editing is done when you setting your binding to textboxes.
as a rule of thumb, if you are using a binding source you always CRUD the data through it. Don't forget to call BindingSource.EndEdit when you are done, hope this helps

How to handle updating a DataGridView when the bound DataSource goes to empty?

I have a DataGridView to which I've set a list of objects to the DataSource. (I'm in VS 2005 using VB.) I created the DataGridView by creating a Data Source of type AssetIdentifier and dragging that Data Source onto my form.
I want to update the DataGridView when the selection in either a combo box or another DataGridView changes. (Below I'm considering a click in another DataGridView.) The following works:
Public Class dlgShowAssets
' class member variable
Private assetIdList As List(Of AssetIdentifier)
' pertinent subs and functions
Private Sub RefreshAssetIdentifierDataGridView()
AssetIdentifierDataGridView.DataSource = assetIdList
End Sub
Private Sub AssetDataGridView_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles AssetDataGridView.CellClick
assetIdList = RepopulateTheList(id)
Me.RefreshAssetIdentifierDataGridView()
End Sub
End Class
In this case, I always knew that assetIdList would have at least one element. I'd update the list, and reset the data source of the DataGridView to that list, and all was well.
When I applied this to another situation, for which I couldn't guarantee that the list would have at least one element, things would work fine as long as I had at least one element in the list, but if the list became empty, the DataGridView threw System.IndexOutOfRangeException a number of times. The rows in the DataGridView would not go away if I went from a non-zero number of elements to zero.
I tried a workaround, which was to remove all of the elements, add one "dummy" element, and then re-bind the list to the control, and it still didn't work.
Also, following all of those exceptions, I'd get other similar exceptions when I hovered over the cells in the DataGridView.
I've been trying to track down this behavior for a few hours. Any insights? Thanks!
Will be happy to add more info if needed.
UPDATE: Some of the members of AssetIdentifier were "Nothing" but I fixed that in the constructor, and the exceptions still occur.
Refactored code and it works ...