Winforms ComboBox SelectedItem changing does not affect the BindingSource

不羁的心 提交于 2019-12-05 04:20:36

I think the problem you are seeing is that the ComboBox is displaying one value, but hasn't written the value to the binding source yet (which doesn't happen until you lose focus).

You can try doing something like this to write the data when an item is selected (assuming there is just the one databinding associated with the ComboBox):

private void comboBox_SelectedIndexChanged(object sender, EventArgs e) {
  comboBox.DataBindings[0].WriteValue();
}

And just to make sure, you either subscribe to this event from the designer, or wire it up manually:

public Form1() {
  InitializeComponent();
  comboBox.SelectedIndexChanged += comboBox_SelectedIndexChanged;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!