Nic*_*ckG 5 c# combobox winforms
我有一个 ComboBox 通过数据源绑定到一个列表。出于某种原因,当数据源项更改时,组合框中的项似乎不会自动更新。我可以在调试器中看到数据源包含正确的项目。
StackOverflow 上有很多关于这个的答案,但大多数要么没有答案,要么对我不起作用,要么需要从使用 Lists 更改为 BindingLists,由于使用 BindingLists 方法的代码量很大,我无法执行此实例有。
当然必须有一种简单的方法来告诉 ComboBox 刷新它的项目?我不敢相信这不存在。我已经有一个在需要更新 Combo 时触发的事件,但我更新值的代码无效。
组合声明:
this.devicePortCombo.DataBindings.Add(
new System.Windows.Forms.Binding("SelectedValue",
this.deviceManagementModelBindingSource, "SelectedDevice", true,
DataSourceUpdateMode.OnPropertyChanged));
this.devicePortCombo.DataSource = this.availableDevicesBindingSource;
Run Code Online (Sandbox Code Playgroud)
更新组合框的代码:
private void Instance_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "AvailableDevices")
{
// Rebind dropdown when available device list changes.
this.Invoke((MethodInvoker)delegate
{
devicePortCombo.DataSource = AvailableDevicesList;
devicePortCombo.DataBindings[0].ReadValue();
devicePortCombo.Refresh();
});
}
}
Run Code Online (Sandbox Code Playgroud)
您没有将 DataGridview 绑定到第一次绑定的DataSource情况下的同一个 BindingSource 对象。this.availableDevicesBindingSource但后来你绑定到不同的对象AvailableDevicesList。您再次使用SelectedValueie的另一个绑定源this.deviceManagementModelBindingSource。
仅使用一个BindingSource即可解决您的问题
| 归档时间: |
|
| 查看次数: |
2321 次 |
| 最近记录: |