简体   繁体   English

C#WinForms ComboBox - 仅选择对象的值

[英]C# WinForms ComboBox - Selecting just the Value from an Object

I just need a little confirmation ... 我只需要一点确认......

I'm filling my combobox like so: 我正在填充我的组合框:

myCombo.Items.Add(new ComboBoxInt32Data("Red", 0));
myCombo.Items.Add(new ComboBoxInt32Data("Green", 1));
myCombo.Items.Add(new ComboBoxInt32Data("Yellow", 2));
myCombo.Items.Add(new ComboBoxInt32Data("Blue", 3));

Now, when I'm retrieving values from the database table, I have just the integer value. 现在,当我从数据库表中检索值时,我只有整数值。

My overriden Equals looks exactly as how msdn suggests : 我的重写Equals看起来与msdn 建议的完全相同:

public override bool Equals(object obj)
{
    if (obj == null)
    {
        return false;
    }

    ComboBoxInt32Data data = obj as ComboBoxInt32Data;
    if ((object)data == null)
    {
        return false;
    }

    return (m_Name.ToUpper() == data.Name.ToUpper() && m_Value == data.Value);
}

But, as I mentioned, I have just the value. 但是,正如我所提到的,我只有价值。 I don't want to have to go and do the following: 我不想去做以下事情:

Int32 DatabaseValue = SomeFunctionThatRetrivedMeThisIntValueFromDB();
string TheValueName = SomeFunctionThatDoesALookUpToGetTheName(DatabaseValue);
myCombo.SelectedIndex = myCombo.Items.IndexOf(new ComboBoxInt32Data(TheValueName, DatabaseValue));

I'd rather just do the following: 我宁愿做以下事情:

myCombo.SelectedIndex = myCombo.Items.IndexOf(SomeFunctionThatRetrivedMeThisIntValueFromDB());

So, my confirmation is, is it OK (as in, is it an acceptable best practice) to do the following in my Equals (look for 'added this'): 因此,我的确认是,在我的Equals中执行以下操作是否正常(如同,是否可接受的最佳做法)(查找“添加此内容”):

public override bool Equals(object obj)
{
    if (obj == null)
    {
        return false;
    }

    // added this
    if (obj is Int32)
    {
        Int32 value = (Int32)obj;
        return m_Value == value;
    }

    ComboBoxInt32Data data = obj as ComboBoxInt32Data;
    if ((object)data == null)
    {
        return false;
    }

    return (m_Name.ToUpper() == data.Name.ToUpper() && m_Value == data.Value);
}

Thanks! 谢谢!

You don't need to override Equal and GetHashCode , the easy way is to cast to ComboBoxInt32Data and use LINQ to select item: 你并不需要重写EqualGetHashCode ,最简单的方式是ComboBoxInt32Data和使用LINQ选择项目:

myCombo.SelectedItem =  myCombo.Items.Cast<ComboBoxInt32Data>()
                          .SingleOrDefault(item => item.Value == databaseValue);

I'm not sure if I'm just misunderstanding your need, or if I'm really seeing what I think I'm seeing, but I think I can simplify your solution quite a bit. 我不确定我是否只是误解了你的需求,或者我是否真的看到了我认为我所看到的内容,但我认为我可以简化你的解决方案。 Let's take a very simple example - I have a table named FooTypes and it contains data like this: 我们来看一个非常简单的例子 - 我有一个名为FooTypes的表,它包含这样的数据:

Id        Name                Order
-----------------------------------
1         Type 1              1
2         Type 2              2
3         Type 3              4
4         Type 4              3

And now I want to load a combo box with that data so I'm going to do something like this**: 现在我想加载一个包含该数据的组合框,所以我要做这样的事情**:

var conn = new SqlConnection("some connection string");
var da = new SqlDataAdapter("SELECT Id, Name FROM FooTypes ORDER BY Order", conn);
var dt = new DataTable();

da.Fill(dt);

comboBox.ValueMember = "Id";
comboBox.DisplayMember = "Name";
comboBox.DataSource = dt;

comboBox.SelectedValue = SomeFunctionThatRetrivedMeThisIntValueFromDB();

** Bear in mind this is an example so you may grab the connection and stuff from somewhere else. **请记住这是一个例子,所以你可以从其他地方获取连接和东西。

Try the following: 请尝试以下方法:

var indexToSelect = SomeFunctionThatRetrievedMeThisIntValueFromDB();
var selectedItem = myCombo.Items
    .Cast<ComboBoxInt32Data>()
    .FirstOrDefault(item => item.Value == indexToSelect);
if (selectedItem != null)
{
    myCombo.SelectedItem = selectedItem;
}

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

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