How to get the value of a control that's bound to a binding source in VB? - sql

I have a Dialog which has a ComboBox (aComboBox, say) with the following properties set:
DataSource linked to a BindingSource which is linked to an SQL table
DisplayMember linked to a column in that table which is of type String
Here is a pic of how the properties are set:
I want the String value of the item selected in the combo box but I am getting System.Data.DataRowViewwhen I use this code at run time:
aComboBox.SelectedItem.ToString()
How do I get the String value?

Do not use aComboBox.SelectedItem.ToString()
Instead of that Use : aComboBox.SelectedValue()

I managed to fix this by setting the Value Member attribute as shown in the following image, I am then able to retrieve the String value by calling aComboBox.SelectedValue.ToString() :
Note: Before, I only had the Display Member attribute set.

Related

Access VBA Combobox Store Value in Column

I have an MS ACCESS Combo Box and I wish to change the value of one of the columns in a particular row. I get error "object required" when I run this line:
Me.ComboName.Column(12, intUseRow) = myVar
(If I am unable to use the above syntax then you should also know that the row I am trying to change is always going to be the "current" visible row so there may be another way of solving the problem due to this fact).
Thanks!
If you have a recordset that is bound to a Table/Query, you will need to change the underlying data then requery the combobox to see changes.
If you load it manually (like in the form load event) and have the comboBox Row Source Type to "Value List" - you should be able to update it like this:
Copy all the data from the selected row into variables.
Combobox.RemoveItem (selected index)
change the required variable to the new value.
construct the semicolon separated string for the value list entry
combobox.AddItem new-string.
a bit messy, but it works correctly!

How to use the value not displayed by a dropdown when connected to access database

My program uses a database in access which consists of BreedID and BreedName. I have a dropdown box in vb.net which shows the name for the user to select, however in my code I would like to store the ID related to that name so that I can send it over to the access database using oledb. Currently this is what it looks like:
Dim BrVar As String = Breed.SelectedItem.Text
But that isn't working! Please help!
You can add hidden columns to your dropdown box, it may already exist. The first column in a dropdown box is column(0) and you can set the width to 0cm. This can be for the ID value. Leaving column(1) for the Name value.
Then you can use Breed.SelectedItem.column(0)
The first thing to do is on the Data tab set up your rowsource to SELECT both the BreedID and BreedName fields (in that order). Then make sure bound column is set to 1.
Then on the Format tab set Column Count to 2 and Column Widths to 0;1.
That will have the result of displaying the BreedName field but using the BreedID field as the value of the combo box.
Use Dim BrVar As Long = Breed.SelectedItem to get the value of BreedID.

change value of field dynamically (OpenERP)

lets say I have a char field under osv.osv_memory
I generate a dynamic form (in "dialog" window) via the fields_view_get method containing this field.
During the generation of the dynamic form, how do I set the value of this field so that when the form is displayed it will contain this value (the value itself depends on the type of processing).
default_get works but only on initialization.
I can change attributes of the field such as readonly, string etc...
But the value itself need to be able to change.
If your osv.osv_memory object is opening from osv.osv, then first create a record of osv_memory object and try to open this record in the wizard. you will not need the default_get function now to load the data to the wizard

Casting problem after obfuscation with Dotfuscator

I'm trying to obfuscate some VB.NET 2003 app.
The resulting assemblyes are obfuscated and "run" with some errors.
I cleaned all potential reflection problems, but I'm not being able to read the selected value of a combobox.
I load the Combobox using their Datasource properties, using a collection of "VTPair" (a class created by me with 2 properties: one of string type and other of object type to store the value)
This combobox handle pairs like "Male | M" or "Female | F".
When trying to see what is selected, I use if mycombo1.SelectedValue = "M" then
This code, after obfuscation, throws me an exception that cannot cast type "XX" to string "M".
So, I changed the code to something more correct, explicitly casting the selected value to String:
if ctype(mycombo1.SelectedValue,string) = "M" then
But the error is the same.
Debugin my original code, the SelectedValue property is of type "Object" but it is a string.
I tried using the ComboBox.SelectedItem property that is also an object but this time what is inside is of type "VTPair" (my own class) and then trying to access its "Value" property (which is of type Object) and trying to cast to string fails again.
Does anyone have an idea to "translate" this piece of code to work OK after Dotfucate it?
Thanks!
From MSDN:
ListControl.SelectedValue Property:
Gets or sets the value of the member property specified by the ValueMember property.
So whatever property NAME you set for the ValueMember property will be used when you use the SelectedValue property. So you may need to exclude from obfuscation, the property which you specify via the ComboBox.ValueMember property.
Not sure of the VB Syntax but in C# I think you would want something where the right-hand side is typeof(MyType). This way the type will be obfuscated to match the renamed type.

Show yes/no in bool column of gridview bound to datasource in Winforms?

I have a gridview that is bound to a datasource (Windows Forms, VB.NET). One of columns is a property of type boolean, and I want to show "yes/no" in the column instead of 0/1 or "true/false". Is this possible? Can you edit displays of columns that are bound?
I have encountered the same problem, unfortunately I didn't find an elegant solution.
Three workarounds are proposed:
Add another property to the data source's class, which returns your string representation of the boolean property. Hide the column showing the boolean value, display the column showing the string value.
Add an unbound string column, populate that column with the appropriate value for each row, and hide the boolean bound column.
Create a wrapper class for your data class, which exposes the properties as you'd like them to be shown in the datagrid.