简体   繁体   中英

Binding a combobox to an enum and passing the values back to a stored procedure

Hello I'm new to C# I apologise if I'm trying to do this in the completely wrong way!

I'm trying to populate a combo box with values from an enum and then pass the value from the enum back to a stored procedure.

As far as displaying the correct items in the combo box it's working fine I get "Order Number" and "Account Number" in the drop down I'm trying to get to a point where the user can select one of the options and I can pass 1 or 2 back to my stored proc.

All of the code relating to the combo box is from other posts I've found on here while trying to sort this out myself.

Currently I'm using -

public enum SearchType
        {
            [Description("Order Number")]
            OrderNumber = 1,
            [Description("Account Number")]
            AccountNumber = 2
        }

public void LoadComboBoxSearchType(ComboBox cbo)
        {
            cbo.DataSource = System.Enum.GetValues(typeof(Customers.SearchType))
            .Cast<System.Enum>()
            .Select(value => new
            {
                (Attribute.GetCustomAttribute(value.GetType().GetField(value.ToString()), typeof(DescriptionAttribute)) as DescriptionAttribute).Description,
                value
            })
            .OrderBy(item => item.value)
            .ToList();

            cbo.DisplayMember = "Description";
            cbo.ValueMember = "value";
        }

So when I'm trying to pass the value from the combobox (expecting 1 or 2) to the stored proc I'm just getting "value".

I'm calling the stored proc like -

    private void ButtonOutboundGridView_Click(object sender, EventArgs e)
    {
        OutboundDatas = DataAccess.GetOutboundDatas(ComboBoxSearchType.ValueMember, TextBoxSearchRef.Text);
    }

Is this a bad way of using Enums, am I over complicating it?

Thanks!

I managed to find a solution to this...figured I'd post it for anyone else who might see this with a similar issue -

I combined the code from this link -

How to bind an enumeration to combobox

with what I already had, to display a description (so it can be formatted a little nicer with a text value) in the combobox but also keep the value as the numeric value from the enum.

public void LoadComboBoxSearchType(ComboBox cbo)
        {
            cbo.DataSource = System.Enum.GetValues(typeof(Customers.SearchType))
           .Cast<Customers.SearchType>()
           .Select(p => new
           {
               Key = (int)p,
               Value = (Attribute.GetCustomAttribute(p.GetType().GetField(p.ToString()),
                       typeof(DescriptionAttribute)) as DescriptionAttribute).Description,
               p
           })
           .ToList();

            cbo.DisplayMember = "Value";
            cbo.ValueMember = "Key";
        }

So now I can use .SelectedValue on my combobox to pass the key (1 or 2) to my stored procedure.

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