简体   繁体   中英

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 :

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'):

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:

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:

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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