Combobox update databinding value - vb.net

I have searched a lot on this topic and cant seem to find what I did wrong so apologies if this has already been answered before.
So I have a combo box with a datasource of Account Types
So I set the properties to
DataSource = 'dscAccountTypeList'
DisplayMember = Name
ValueMember = Id
Now I bind this combobox to my datatable via code
cmdAccountType.DataBindings.Add("SelectedValue, dtMaster, "AccountTypeId", True, DataSourceUpdateMode.OnPropertyChanged)
My problem is the AccountTypeId on dtMaster does not update even if I changed the selected item on the combobox. The above method works on textboxes which updates the value on the datatable once the text has been changed. Sure I can set value via Code on change of the combobox but I am wondering why it doesn't automatically update like the textboxes. I already tried clicking on other field to fire the change event but it didn't work. I would appreciate it if anyone can point me in the right direction. Thanks!
P.S. the selected item on the combo box changes once the value on the dtMaster changed. It just doesnt work on the opposite.

Set your DataSource after you set your DisplayMember and ValueMember, not before.
DisplayMember = Name
ValueMember = Id
DataSource = 'dscAccountTypeList'

Related

Setting focus to another control prevents current control value from changing in VB.NET

I have a ComboBox in a WinForms app written in VB.NET. In the .SelectionChangeCommitted event I want to change the focus to a different specific control to assist user workflow. However when I do that, the change is not saved on the initial ComboBox and the value & index are reverted to the original values.
I've used both myControl.Focus and myControl.Select
The Combobox is setup like this:
With ChoosePartType
.DisplayMember = "PartName"
.DataSource = GetTable(qry) 'This custom function returns a DataTable with fields PartNum and PartName
.ValueMember = "PartNum"
.SelectedIndex = -1
End With
I assume something in the changing focus is short-circuiting the property change. Is there a way to force that to happen before I change focus?
Note: seems like a different issue from WInforms Combobox SelectionChangeCommitted event doesn't always change SelectedValue
Similar to this but I don't use databindings: Combobox DataBinding Bug - Won't write value if programmatically losing focus
Try using:
ChoosePartType.SelectedItem.Row("PartNum")
Assuming that this is the first column of the control's datasource, this could be also written as:
ChoosePartType.SelectedItem.Row(0)

When binding both the Items and SelectedValue to aListBox I seem to get a race condition, how to fix?

For a legacy project, I have a WinForm with a BindingSource1 bound to a strongly typed SQL 2005 DataSet. There is a BindingSource2 bound to the first on a relation.
When selecting a Row in DataGridView, I'd like to present a ListBox with all the child rows with one Item selected based on a value in the selected Row. I bound the DataGridView to BindingSource1, the ListBox' Datasource is BindingSource2, ValueMember and DisplayMember are set to the primary key and a description respectively. I added a Databinding of SelectedValue to the foreign key in BindingSource2.
On Load of the Winform and when the selected row changes by changing the sort order, everything is fine, all related rows show up in the ListBox and the correct item is highlighted.
When I click into the DataGridView, .Net seems to change the SelectedValue of the ListBox and repopulate its items after that. In Effect, when the newly selected item is already in the ListBox it gets highlighted, then the ListBox is filled with the new items and the first one is selected (SelectedValue is reset).
Is there any .EndEdit, .CancelSomething or .UpdateFoo that I am missing? How to ensure the internal sequence of Items.Clear(), (fill), and SelectedValue = x? I am stripping down the problem to a minimal project but if anyone has an idea I'd like to skip that part ; )
Added: All Bindings are set using the Designer of Visual Studio 2008 Pro, the project is VB.Net Framework 2.0 Winform.
I am documenting my workaround to further describe the problem I have. It might help others and possibly lead to a real solution.
Like in "How to bind the "SelectedValue" property of a combobox that gets his elements from a datasource?" the control is filled by DataSource, DisplayMember and ValueMember plus has a DataBinding of its SelectedValue. There seems to be a problem: In ListBox and ComboBox the Position of the DataSource (BindingSource2 in my example) define the current selected Item, binding SelectedValue to another DataSource leads to erratic behaviour.
For my workaoround I bound ListBox.DataSource to a DataView wich is filled / filtered in BindingSource2_ListChanged so the DataSource of the ListBox has no Position to use as SelectedItem / Index / Value. It then uses the DataBinding only and my UI shows the expected Behaviour.

VB.NET Bound ComboBox SelectedValue does not display

I bind a datatable to the combobox.DataSource on load. I then give the combobox a DisplayMember and a ValueMember (2 different columns out of the datatable). In the SelectedIndexChanged of the combobox I would like to use the SelectedValue property of the combobox, just to test I MsgBox(combobox.SelectedValue) and I get "Argument 'Prompt' cannot be converted to type 'String'." Why isn't it displaying the value? :(
OnLoad
cbCISoftware.DataSource = dbMaps.Tables("maps")
cbCISoftware.ValueMember = "id"
cbCISoftware.DisplayMember = "name"
SelectedIndexChanged of cbCISoftware
MsgBox(cbCISoftware.SelectedValue)
SelectedValue.ToString outputs
System.Data.DataRowView
I believe the issue is that you need to bind the table's DefaultView:
cbCISoftware.DataSource = dbMaps.Tables("maps").DefaultView
First of all you have to be sure to have selected DropDownList as DropDownStyle for the Combobox and that the binding is working.
Then you have to replace MsgBox(cbCISoftware.SelectedValue)
with MsgBox(cbCISoftware.SelectedValue.ToString)
Otherwise to obtain the result, MsgBox(cbCISoftware.Text) will work, but it not probably what you are looking for :-)
I can provide you with complete code to do the binding if you need it.

Set dropdownlist in gridview in rowediting event

Trying to get the findcontrol to work so I can set the default value of the dropdownlist I have in a template field, but I'm having no luck. Am I doing something wrong?
Dim drdList As DropDownList
For Each row As GridViewRow In gridviewComputer.Rows
drdList = gridviewComputer.Rows(e.NewEditIndex).FindControl("statusDropDown")
Next
Found an easier way.
Go into the gridview template editing, then under the EditItemTemplate, where I had the dropdownlist declared I selected Edit DataBindings. Refresh the Schema, then I was able to set the SelectedValue property to the corresponding field of my gridviews datasource. (Which really just fills in "Bind("columnName")" in the Code expression.) Problem solved!

Changing styling of DataGridViewComboBoxCell from DropDownList to DropDown?

(This question is narrower in scope than this question I asked earlier.)
Say I have a DataGridViewComboBoxColumn, and want to switch the style of the ComboBox control between DropDownList and DropDown (mainly for the text field editing capability). I'd like to do this on a row-by-row basis (at the DataGridViewComboBoxCell level).
How would I do that programatically?
Or maybe a different way of asking: How do I access the ComboBox control given an object of type DataGridViewComboBoxCell?
(I'm in VB 2005.)
Thanks as always.
Not sure if you still need an answer, but i have a question that covers similar ground: Detect which column is showing an editing control in a datagridview
i use something like this line to get the combobox out of the DataGridView (DGV) in my DataGridView's CellValidating event:
Dim comboBox As DataGridViewComboBoxCell = DGV.Item(e.ColumnIndex, e.RowIndex)
later i use this line to get change the DropDownList ComboBoxCell to a DropDown:
cb.DropDownStyle = ComboBoxStyle.DropDown
For mine to work i had to make sure the 'cb' was of the ComboBox type, i dont remember if i was able to get it working well if the combobox was of the DataGridViewComboBoxCell type.