组合框已清除后,该值() [英] ComboBox has its old value after Clear()

查看:175
本文介绍了组合框已清除后,该值()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个组合框 cb_Brand cb_Model 上一个WinForm。

I have two comboBox cb_Brand and cb_Model on a winForm.

cb_Model 填充在品牌选择的值。 问题是:如果我们选择品牌任何和选择品牌下的任何模型, cb_Model 不松动的previous模式选择的值。 例如:如果我们选择了奥迪品牌和型号A3 和选择品牌的福特,当我点击 cb_Model 来选择模型,它显示了A3作为选择的模型,但在列表中仍然其他车型都属于福特。

cb_Model populates values on brand Select. the problem is: if we select the brand any and select the any model under that brand, cb_Model does not loose the value of previous model selected. for example: If we select the brand Audi and model A3 and the select the Brand Ford, when I click on cb_Model to select the model, it displayed the A3 as selected model, but still other models in list are belong to ford.

我的code是:

private void cb_Brand_SelectedIndexChanged(object sender, EventArgs e)
{
    // Clear Current Data
    cb_Model.Text = "";
    cb_Model.Items.Clear();

    CarModel _carmodel = new CarModel ();

    // Get Selected Car Brnad
    int CarBrandID = _carmodel .GetCarBrandID(cb_Brand.Text);

    //Enable choice of Model
    SortedList<int, Model> colM;

    colM = Model.ReadModel(CarBrandID);

    cb_Model.DisplayMember = "ModelText";
    foreach (Model objM in colM.Values)
    {
        cb_Model.Items.Add(objM);
    }
}

任何想法,请.. 谢谢

Any Idea Please.. Thanks

无法找到原因,但整理出来了临时修复:

unable to find the reason but sorted out with a temp fix:

private void cb_Model_Click(object sender, EventArgs e)
{
    cb_Model.Text = "";
}

非常感谢球员 欢呼声

Thanks a lot guys cheers

推荐答案

foreach (Model objM in colM.Values)
{
    cb_Model.Items.Add(objM);
}

让.NET照顾它为你和它与此替换:

Let .NET take care of it for you and replace it with this:

cb_Model.DataSource = colMValues;

这将数据绑定到列表并automatcially刷新组合框的物品时的数据源被设置

Which will bind the data to the list and refreshes the comboboxes items automatcially when a data source is set.

您也将不再需要这些行:

You will also not need these lines anymore:

// Clear Current Data
cb_Model.Text = "";
cb_Model.Items.Clear();

对此有阅读有关绑定列表的详细信息(和其他数据源),以组合框​​:

Have a read of this for more info on binding lists (and other data sources) to ComboBoxes:

如何:绑定Windows窗体组合框或列表框控件数据(MSDN)

这篇关于组合框已清除后,该值()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆