简体   繁体   English

select combobox 项目价值 C#

[英]select combobox item by value C#

I have a combobox, bound to Datatable and have the following properties:我有一个Datatable ,绑定到数据表并具有以下属性:

cboCars.DisplayMember = "carLiscen";
cboCars.ValueMember = "carNo";

How can I select the DisplayMember when I know the ValueMember ?当我知道ValueMember时,我怎样才能 select DisplayMember

If you have a ValueMember set you can select using SelectedValue 如果您设置了ValueMember ,则可以使用SelectedValue进行SelectedValue

cboCars.DisplayMember = "carLiscen";
cboCars.ValueMember = "carNo";

cboCars.SelectedValue = "valuemember value";

You can use cboCars.SelectedValue = "123"; 你可以使用cboCars.SelectedValue = "123"; property for this. 这个属性。 Here's a code snippet which will show it in action. 这是一个代码片段,可以显示它的实际效果。

    public void Test()
    {
        ArrayList info = new ArrayList();
        info.Add(new CarInfo { CarLiscen = 123456, CarNo = 123});
        info.Add(new CarInfo { CarLiscen = 234567, CarNo = 234 });
        cboCars.DataSource = info;

        cboCars.DisplayMember = "CarLiscen";
        cboCars.ValueMember = "CarNo";

        cboCars.SelectedValueChanged +=
            delegate(object sender, EventArgs e)
            {
                if (cboCars.SelectedIndex != -1)
                {
                    this.Text = cboCars.SelectedValue.ToString();
                }
            };

        cboCars.SelectedValue = 234;
    }

And if you wonder what is the definition of CarInfo . 如果你想知道CarInfo的定义是什么。 Here's its code (which is fairly simple): 这是它的代码(非常简单):

public class CarInfo
{
    public int CarLiscen { get; set; }
    public int CarNo { get; set; }
}

Hope this helps. 希望这可以帮助。

You can search for the correct item and set it to that, very simple: 您可以搜索正确的项目并将其设置为非常简单:

cbTEST.SelectedIndex = cbTEST.FindStringExact("your search string here");

or select an item based on an ListViewItem: 或者根据ListViewItem选择一个项目:

cbTEST.SelectedIndex = cbTEST.FindStringExact(lvTEST.SelectedItems[0].SubItems[0].Text);

thats it. 而已。 very simple! 很简单!

Hi Guys the best way if searching for a text or value is 嗨,如果搜索文本或值,最好的方法是

int Selected;    
int count = ComboBox1.Items.Count;
    for (int i = 0; (i<= (count - 1)); i++) 
     {        
         ComboBox1.SelectedIndex = i;
        if ((string)(ComboBox1.SelectedValue) == "SearchValue") 
        {
            Selected = i;
        }

    }

    ComboBox1.SelectedIndex = Selected;

The problem is that the combobox expect the exact type.问题是 combobox 需要确切的类型。 So if you for example use a datagridview and you see the value (int) in the field, you pass it to the SelectedValue property of the combo.因此,例如,如果您使用 datagridview 并且在字段中看到值 (int),则将其传递给组合的 SelectedValue 属性。 But in fact you are not passing an integer, you are passing an object. That is what usually is going wrong.但实际上你没有传递 integer,你传递的是 object。这通常是出错的地方。 It my a while to sort this out, but finally I have found how to do it... How to solve this... easy:我花了一段时间来解决这个问题,但最后我找到了如何去做......如何解决这个......简单:

For example, you have an ID (integer in the database), then you need to do something like this:例如,你有一个ID(数据库中的整数),那么你需要做这样的事情:

This is the one that I did and it does not work: cmbFilter.SelectedValue = dgvListOfFilters.Rows[intRowSelected].Cells[3].Value;这是我所做的,但它不起作用:cmbFilter.SelectedValue = dgvListOfFilters.Rows[intRowSelected].Cells[3].Value;

You should do this to make it work: int intCategory = Convert.ToInt32(dgvListOfFilters.Rows[intRowSelected].Cells[3].Value);您应该这样做以使其工作: int intCategory = Convert.ToInt32(dgvListOfFilters.Rows[intRowSelected].Cells[3].Value); cmbFilter.SelectedValue = intCategory; cmbFilter.SelectedValue = intCategory;

Hope this will be helpfull for many people...希望这对很多人有帮助......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM