Winforms 组合框 SelectedValue 为空 [英] Winforms Combobox SelectedValue is Null

查看:25
本文介绍了Winforms 组合框 SelectedValue 为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据输入表单,上面有几个组合框.每个组合框都有自己的绑定源,当我启动表单时它会正确填充.但是,如果我编辑表单上的记录并尝试将来自组合框的更新的 selectedvalue 传递给数据库,我会收到一个错误,即我传递给它的值为 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)
);

任何帮助将不胜感激.

推荐答案

您是否正在使用 DropDownDropDownStyle 并在 ComboBox 中键入内容?如果是这样,那将导致 SelectedValue 为空,我假设是因为输入的值不再是 ComboBox 中的项目之一.

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.

如果是这种情况,假设用户必须选择现有值,请将 DropDownStyle 设置为 DropDownList.

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

这篇关于Winforms 组合框 SelectedValue 为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆