C# 当我写东西时,如何停止绑定源以查找数据

C# 当我写东西时,如何停止绑定源以查找数据,c#,winforms,combobox,C#,Winforms,Combobox,我的问题是,当我在文本框中写东西时,我的组合框会清除它的选择。这只发生在一个特定的组合框上,该组合框使用下面的代码进行数据绑定 private void FillEmployemenetType() { var items = new BindingList<KeyValuePair<string, string>>(); items.Add(new KeyValuePair<string, string>("C", "Contract"))

我的问题是,当我在文本框中写东西时,我的组合框会清除它的选择。这只发生在一个特定的组合框上,该组合框使用下面的代码进行数据绑定

private void FillEmployemenetType()
{
    var items = new BindingList<KeyValuePair<string, string>>();

    items.Add(new KeyValuePair<string, string>("C", "Contract"));
    items.Add(new KeyValuePair<string, string>("P", "Permanent"));
    items.Add(new KeyValuePair<string, string>("V", "Vacation"));

    contractTypeComboBox.DataSource = items;
    contractTypeComboBox.ValueMember = "Key";
    contractTypeComboBox.DisplayMember = "Value";
    contractTypeComboBox.SelectedIndex = 0;
} 
private void fillEmploymentType()
{
var items=newbindingList();
添加(新的KeyValuePair(“C”、“合同”));
添加(新的KeyValuePair(“P”、“永久”));
添加(新的KeyValuePair(“V”、“假期”));
contractTypeComboBox.DataSource=项目;
contractTypeComboBox.ValueMember=“Key”;
contractTypeComboBox.DisplayMember=“值”;
contractTypeComboBox.SelectedIndex=0;
} 
我没有触发任何文本更改事件或为此编写任何代码。
我将绑定源用于此窗口窗体,以防出现问题。

您可以在设置数据源后尝试设置
SelectedItem
,以便重新选择上一项

private void FillEmployemenetType()
{
    KeyValuePair<string,string>? previous = null;
    if (contractTypeComboBox.DataSource != null) {
        previous = (KeyValuePair<string,string>)contractTypeComboBox.SelectedItem;
    }

    var items = new BindingList<KeyValuePair<string, string>>();

    items.Add(new KeyValuePair<string, string>("C", "Contract"));
    items.Add(new KeyValuePair<string, string>("P", "Permanent"));
    items.Add(new KeyValuePair<string, string>("V", "Vacation"));

    contractTypeComboBox.DataSource = items;
    contractTypeComboBox.ValueMember = "Key";
    contractTypeComboBox.DisplayMember = "Value";
    //contractTypeComboBox.SelectedIndex = 0;
    if (previous.HasValue) {
        try {
            contractTypeComboBox.SelectedItem = previous;
        }
        catch { }
    }
} 
private void fillEmploymentType()
{
KeyValuePair?previous=null;
if(contractTypeComboBox.DataSource!=null){
previous=(KeyValuePair)contractTypeComboBox.SelectedItem;
}
var items=newbindingList();
添加(新的KeyValuePair(“C”、“合同”));
添加(新的KeyValuePair(“P”、“永久”));
添加(新的KeyValuePair(“V”、“假期”));
contractTypeComboBox.DataSource=项目;
contractTypeComboBox.ValueMember=“Key”;
contractTypeComboBox.DisplayMember=“值”;
//contractTypeComboBox.SelectedIndex=0;
if(上一个.HasValue){
试一试{
contractTypeComboBox.SelectedItem=previous;
}
捕获{}
}
} 

我读了两遍,但看不懂其中的意思question@USER_NAME问题是我有一个组合框,我用上面的代码绑定它。在我的windows窗体上,我对所有文本框和组合框使用绑定源代码。问题是,当我在文本框中自动写入内容时,我需要再次选择组合框。这只发生在我用上述代码绑定的
组合框上。这里我也很清楚,我没有在我的文本框上触发任何关于
textchange
的事件,所以为什么会发生这种情况呢?好吧,如果这是组合框的绑定代码,那么您显然是在创建一个新列表并将其设置为数据源,并且在设置
SelectedIndex
为0的基础上,将第一项设置为选中。在分配数据源之前,您需要存储什么是
SelectedItem
,并在设置数据源之后重置它。@Patrick那么,您能给我举个例子说明如何做到这一点吗?您好,在
KeyValuePair previous=(KeyValuePair)contractTypeComboBox上给出一个异常异常是对象引用而不是设置ObjectOk的实例好的,您应该能够得出“它不起作用”是不够的信息,而nullreference异常也没有说那么多。下次,如果可以,请指定哪个对象为空。我用我认为是问题的东西编辑了代码。