简体   繁体   English

在Winform中按值c#选择组合框项目

[英]select combobox item by value c# in winform

I have the below custom class for my combobox, and i'm trying to programmatically select an item by value. 我的组合框具有以下自定义类,并且我试图通过编程方式选择一个值。 I'm able to retrieve the item by value using casting, but can't seem to select the item by value. 我可以使用强制转换按值检索项目,但似乎无法按值选择项目。

string test2 = ((GetAcctNum)_comboBox.SelectedItem).Value; 

// casting can retreive the item by value, but how do you select the item by value? //强制转换可以按值检索项目,但是如何按值选择项目?

public class GetAcctNum
{
    public string Value { get; set; }
    public string Text { get; set; }
    public override string ToString()
    {
        return Text;
    }
}

I cant use the below solution, because I won't know the text. 我无法使用以下解决方案,因为我不知道文本。 I will only know the hidden value that needs to be selected in the combobox. 我只会知道需要在组合框中选择的隐藏值。

_comboBox.SelectedValue = _comboBox.Items.FindByText("text").Value;

Below is how I add the values to the comboBox 下面是我如何将值添加到comboBox

SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
 {
     while (reader.Read())
    {
       temp = new GetAcctNum();
       temp.Value = reader["AccountID"].ToString();
       temp.Text = reader["AccountNumber"].ToString();  
        getAcctNum.Add(temp);
        rowsAffected++;
   }
 }
//Omitted for brevity//
foreach (GetAcctNum item in getAcctNum)
{
   _comboBox.Items.Add(item);
}

The getAcctNum getAcctNum

When you assign the list of account numbers to your ComboBox , specify which property to display to the user, and which to use as the underlying value of the selection. 当您将帐号列表分配给ComboBox ,请指定向用户显示哪个属性,以及将哪个属性用作选择的基础值。

List<GetAcctNum> accountNumbers = GetAccountNumbersFromWherever();

_comboBox.DataSource = accountNumbers;
_comboBox.DisplayMember = "Text";
_comboBox.ValueMember = "Value";

Then you can set the SelectedValue by specifying one of the values from your list of accounts. 然后,您可以通过指定帐户列表中的值之一来设置SelectedValue

_comboBox.SelectedValue = "SomeValueFromYourListOfAccounts";

Use this class and method to select item by value. 使用此类和方法按值选择项目。

    public class ComboboxItem
    {
        public string Text { get; set; }
        public object Value { get; set; }

        public ComboboxItem(string text, string value)
        {
            Text = text;
            Value = value;
        }
        public override string ToString()
        {
            return Text;
        }
    }


    private  void SelectCmbItemByValue( ComboBox cbo, string value)
    {
        for (int i = 0; i < cbo.Items.Count; i++)
        {

            ComboboxItem ci = (ComboboxItem)cbo.Items[i];
            string _value = ci.Value.ToString();
            if (ci != null && _value == value)
            {
                cbo.SelectedIndex = i;
                break;
            }
        }

    }

Than you, this helped me a lot 比你,这对我帮助很大

that worked 那工作

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

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