简体   繁体   English

组合框的C#代码设置为可选值?

[英]c# code for combobox to set as optional value?

c# code for combobox to set as optional value? 组合框的C#代码设置为可选值? I Have 3 Dev Express combo box,I need one combo box as optional how to achieve this? 我有3个Dev Express组合框,我需要一个组合框作为可选件如何实现?

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. 并将comboBox更改为所需的comboBox名称。

Of course you can change " " for every string you like - just be sure it is unique from real choices 当然,您可以为每个喜欢的字符串更改" " -一定要确保它在实际选择中是唯一的

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

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