简体   繁体   中英

Winforms Combobox SelectedValue is Null

I have a data entry form that had several comboboxes on it. Each of the combo boxes has its own binding source and it is populating correctly when I launch the form. However, if I edit the record on the form and try to pass to the database the updated selectedvalue from the combobox I get an error that the value I am passing it NULL.

This issue seems to happen every other time I run the darn thing and I can't figure out why it is not working properly. Basically I am trying to insert a new record into a table but for some reason it is failing to pull my selected value. My code to insert the data is below:

Data.Manager.AddEmployee
 (
   InactiveEmployeeSelected.GUID,
   Convert.ToByte(RoleComboBox.SelectedValue),
   NotesTextBox.Text.Trim(),
   ScheduleTextBox.Text.Trim(),
   ExtensionTextBox.Text.Trim(),
   CodeTextBox.Text.Trim(),
   Convert.ToBoolean(EBApprovedCheckbox.CheckState),
   Convert.ToByte(ApprovalLevelComboBox.SelectedValue),        //pulling null
   Convert.ToBoolean(AssignComtracksCheckbox.CheckState),
   Security.Manager.CurrentUser.GUID,
   DateTime.Today,
   Convert.ToBoolean(IsActiveCheckbox.CheckState)
);

Any help would be greatly appreciated.

也许,你只需要使用SelectedItem

Are you using a DropDownStyle of DropDown and typing into the ComboBox? If so, that will cause SelectedValue to be null, I'm assuming because the value entered is no longer one of the items in the ComboBox .

If this is the case, set the DropDownStyle to DropDownList , assuming the user has to pick an existing value.

ComboBox has to have DataSource. Link data though ComboBox1.DataSource

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