在绑定组合框中空空项目 [英] NULL Empty Item in Bound ComboBox
问题描述
我怎么可以在绑定组合框
,它使用 NULL
作为一个插入值或空项更新?
通过下面的code,我可以手动添加其他行。列 inspector_id
是一个FK关系的主键。我必须设置 inspector_id = -1
,因为C#不允许的 INT
是空
。然而,插入(或更新)失败,因为没有 inspector_id:-1
数据库
私人无效ItemInfo_Load(对象发件人,EventArgs的)
{
// TODO:这行code加载数据到someDBDataSet.inspector'表。您可以移动,或将其删除,因为需要的。
this.inspectorTableAdapter.ClearBeforeFill = FALSE;
someDBDataSet.inspectorRow NEWROW = this.someDBDataSet.inspector.NewinspectorRow();
newRow.inspector_id = -1; //由于在C#中int类型不能为空
newRow.fullName =(无);
newRow.employee code =;
this.someDBDataSet.inspector.AddinspectorRow(NEWROW);
this.inspectorTableAdapter.Fill(this.someDBDataSet.inspector);
//this.inspectorTableAdapter.ClearBeforeFill = FALSE;
// TODO:这行code加载数据到someDBDataSet.item'表。您可以移动,或将其删除,因为需要的。
this.itemTableAdapter.Fill(this.someDBDataSet.item);
}
尤里卡!绑定到一个视图,而不是表。
绑定 inspector_idComboBox
的检查表中的一个新的SQL Server视图。
SELECT NULL作为inspector_id,(无)作为全名,为员工code
联盟
SELECT inspector_id,全名,员工code
从dbo.inspector
优点:
- 的
(无)
产品在组合框
在选择项目时 - 的
的SelectedItem
和文本仍然存在。 - 在SQL视图允许空值
inspector_id
- 没有解决方法都需要在应用程序code。只需填写
数据集
从视图。 - 允许更多的灵活性,由于工作的关系未绑定。
...辉煌!
How can I have an empty item in the bound ComboBox
which uses NULL
as the value for an Insert or Update?
With the code below, I can manually add the additional row. The column inspector_id
is the primary key of an FK relationship. I have to set inspector_id = -1
, since C# does not allow an int
to be null
. However, the insert (or update) fails since there is no inspector_id: -1
in the database.
private void ItemInfo_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'someDBDataSet.inspector' table. You can move, or remove it, as needed.
this.inspectorTableAdapter.ClearBeforeFill = false;
someDBDataSet.inspectorRow newRow = this.someDBDataSet.inspector.NewinspectorRow();
newRow.inspector_id = -1; // Since an int in C# cannot be null
newRow.fullName = "(none)";
newRow.employeeCode = "";
this.someDBDataSet.inspector.AddinspectorRow(newRow);
this.inspectorTableAdapter.Fill(this.someDBDataSet.inspector);
//this.inspectorTableAdapter.ClearBeforeFill = false;
// TODO: This line of code loads data into the 'someDBDataSet.item' table. You can move, or remove it, as needed.
this.itemTableAdapter.Fill(this.someDBDataSet.item);
}
Eureka! Bind to a view, not table.
Bind inspector_idComboBox
to a new SQL Server view of the inspector table.
SELECT NULL as inspector_id, '(none)' as fullName, '' as employeeCode
UNION
SELECT inspector_id, fullName, employeeCode
FROM dbo.inspector
Pros:
- The
(none)
item is in theComboBox
- The
SelectedItem
and text persists when selecting the item. - The SQL view allows a NULL value for
inspector_id
- No workarounds are needed in the application code. Just fill the
DataSet
from the view. - Allows more flexibility as the relationship is not bound.
... brilliant!
这篇关于在绑定组合框中空空项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!