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

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.

Related

Datagridview Invisible Columns - accessing data

Having set up a datagridview, bound to a bindingsource, I have to use the cellformatting to paint the cells various colours.
Since you cannot access a field or record via bindingsource on a cellformatting event, the only way I could access the data, is through a column such as the primary ID for example. (this is only for cellformatting, i have no problem accessing the ID through the bindignsource.current object).
So, once I have finished designing and testing my datagridview, I then switch various columns (ID) for this example, to visible=false to make the column invisible.
I now cannot access the column data through cellformatting. I have read on a number of pages listed by google that since datagridview doesnt render the columns, the values are unobtainable aside from a couple of alternatives such as :
.TAG
and datakeynames
I cannot find datakeynames under datagridview for winforms for some reason.
.tag doesnt seem to do anything as far as I can tell.
Since trying to use this from the cell formatting, I am unable to use
Select Case .Columns(e.ColumnIndex).DataPropertyName.ToLower
or
If Datagridview1.columns(e.columnindex).datapropertyname.tolower='id' Then
as the column is invisible...
Shame I cannot access the bindingsource through cellformatting, but there must be a way and there must be others who have had this issue.
Use DataBoundItem:
Dim drw As DataRow = DirectCast(DataGridView1.Rows(i).DataBoundItem, DataRowView).Row
(Assuming you've bound to a DataTable)

change the list of combobox A based on the item chosen from combobox B

I am new to vb.net and I am using visual studio 2010. I have two comboboxes on a form, each combobox is set to DropDownList so that a list of items can show in the combobox, but no text is allowed to be entered.
For combobox A, if user chooses item 1, the list of combobox B should be updated accordingly. I think this is quite common on a lot of applications. But I do not know to implement it. I even do not know the keyword to search for the relevant property or event handler.
Thanks a lot!
Take a look at SelectedValueChanged. You can subscrive to this event on your first combobox, then when the event is raised, you can combo2.Items.Clear () the 2nd collection, then add your items.

problem with selecting a rows in advanced datagrid using keydown event

I have advanced datagrid with list of items is displayed.I am using keydown for selecting the rows.But the actual problem is when i am selecting the items after the 2nd item it should move to the 3rd item but instead of that its moving back to the 1st item.I have defined selectionmode property to singlerows even then the result same to be same.can any one give me the soloution.
i have resolved this issue..it was related to binding problem what we had in datgrid.

How to make custom Combo box control which have contain datagrid in vb.net

I want to make custom combo box which have contain datagrid and textbox too.
On clicks into combo box datagridview should be appear and selecting any row particular cell value of the datagridview added on the combo.
thanks in advance..
As your question is written I have no idea how to answer it, I can't figure out how you could sensibly show a DataGridView inside a ComboBox. I'm writing this answer with the assumption that you mean that the form should have a ComboBox that are "linked" to a DataGridView rather than actually contain it.
If so, all you need to do is to add the ComboBox and DataGridView to the form, make the DataGridView invisible. Then you handle the SelectedIndexChanged event for the ComboBox, in the handler, make the grid visibile, find the index of the row and column you want to show and write code like (code not tested so might not be correct):
grid.SelectedRows.Clear()
grid.FirstDisplayedScrollingRowIndex = rowIndex
grid.Rows(rowIndex).Cells(colIndex).Selected = True
grid.Show()

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.