C# - getting value of SelectedItem in Windows Forms application

32,809

Solution 1

I know I am a little late, but this works well:

int? nStoreNumber = cmbABSM.SelectedValue as int?;
if (nStoreNumber==null)
    return;

Solution 2

Try using SelectedValue if ValueMember is set for the combobox, otherwise default to the Text property:

//If ValueMember is set
int nStoreNumber = Convert.ToInt32(cmbABSM.SelectedValue);

//Otherwise
int nStoreNumber = Convert.ToInt32(cmbABSM.Text);

Either way I would recommend you make sure the value of what is selected is a valid int.

int nStoreNumber;

if (!int.TryParse(cmbABSM.SelectedValue, out nStoreNumber))
{
    //This is not a valid number.  Notify the user.
}

Solution 3

does

Int32.Parse(box.SelectedItem.ToString());

work for you?

Solution 4

You could use SelectedItem.Value or SelectedValue, the practical difference is in what they return when there is no selection.

SelectedItem.Value returns the value, will return null if there is no selected item.

SelectedValue also returns the value, but will return an empty string if there is no selected item

Further reading:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.selecteditem.aspx

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.selectedvalue.aspx

Share:
32,809
KalC
Author by

KalC

An eager learner and always thriving to become the jack of all trades.

Updated on July 05, 2022

Comments

  • KalC
    KalC almost 2 years

    I have a simple Windows Forms application (with an Access database) with a combobox (cmbStores) that is populated in the most simple way imaginable.

    Problem: I am unable to get the value of the selected item.

    Here is how I am populating this combobox:

    // Variable declaration
            string strQueryStores = "SELECT StoreNumber FROM tblStoresAndRegion ORDER BY StoreNumber";
            string strConnectionString = UtilityClass.GetConnectionString();
            OleDbConnection connStores;
            OleDbDataReader readerStores = null;
    
            connStores = new OleDbConnection(strConnectionString);
    
            try
            {
                connStores.Open();
                OleDbCommand sqlGetStores = new OleDbCommand(strQueryStores, connStores);
    
                cmbStore.Items.Clear();
                cmbStore.Items.Add("All");
                if (connStores != null)
                {
                    readerStores = sqlGetStores.ExecuteReader();
    
                    if (readerStores.HasRows)
                    {
                        while (readerStores.Read())
                        {
                            cmbStore.Items.Add (Convert.ToInt32(readerStores["StoreNumber"]));
                        }
                    }
                }
                cmbStore.SelectedIndex = 0;
    
            }
    
            catch (OleDbException oledblEX)
            {
                MessageBox.Show(oledblEX.Message);
            }
    
            finally
            {
                if (readerStores != null)
                    readerStores.Close();
                if (connStores != null)
                    connStores.Close();
            }
    

    This is how I am trying to get the value of the selected item.

    int nStoreNumber = Convert.ToInt32(cmbABSM.SelectedItem);