简体   繁体   中英

c# code for combobox to set as optional value?

c# code for combobox to set as optional value? I Have 3 Dev Express combo box,I need one combo box as optional how to achieve this?

My code is below:

         if (cmbEmployeeIDName.SelectedItem != null)
            {
                EmployeeId = Convert.ToInt64(cmbEmployeeIDName.SelectedItem.Value);
            }

            if (cmbCompany.SelectedItem != null)
            {
                CompanyId = Convert.ToInt64(cmbCompany.SelectedItem.Value);
            }
            if (cmbDepartment.SelectedItem != null)
            {
                DepartmentId = Convert.ToInt64(cmbDepartment.SelectedItem.Value);
            }

you can compare with empty string

Try this:

if (cmbEmployeeIDName.SelectedItem !=null && cmbEmployeeIDName.SelectedItem.Value.Trim() != "")
{
    EmployeeId = Convert.ToInt64(cmbEmployeeIDName.SelectedItem.Value);
}

First you need to add default value to your Combobox like this

comboBox.Items.Add(" "); 

Then, when you check for assignment:

if (comboBox.SelectedItem != null && comboBox.SelectedItem.Value != " ")
            {
                comboBoxId = Convert.ToInt64(comboBox1.SelectedItem.Value);
            }

And change comboBox to desired comboBox name.

Of course you can change " " for every string you like - just be sure it is unique from real choices

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