0

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.

4

1 回答 1

0

我正在记录我的解决方法以进一步描述我遇到的问题。它可能会帮助其他人,并可能导致真正的解决方案。

就像在“如何绑定从数据源获取其元素的组合框的“SelectedValue”属性? ”中一样,该控件由 DataSource、DisplayMember 和 ValueMember 填充,并且具有其 SelectedValue 的 DataBinding。似乎有一个问题:在 ListBox 和 ComboBox 中,数据源的位置(在我的示例中为 BindingSource2)定义了当前选定的项目,将 SelectedValue 绑定到另一个数据源会导致行为不稳定。

对于我的解决方法,我将 ListBox.DataSource 绑定到在 BindingSource2_ListChanged 中填充/过滤的 DataView,因此 ListBox 的 DataSource 没有用作 SelectedItem / Index / Value 的位置。然后它只使用 DataBinding,我的 UI 显示了预期的行为。

于 2013-09-20T11:13:55.800 回答