绑定的组合框中的NULL空项[英] NULL Empty Item in Bound ComboBox

本文是小编为大家收集整理的关于绑定的组合框中的NULL空项的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

本文来自:IT宝库(https://www.itbaoku.cn)

我如何在bount ComboBox中使用一个空的项目,该项目使用NULL作为插入或更新的值?

使用以下代码,我可以手动添加附加行.列inspector_id是FK关系的主要键.我必须设置inspector_id = -1,因为C#不允许int为null.但是,插入(或更新)失败,因为数据库中没有inspector_id: -1.

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!绑定到视图,而不是表.

将inspector_idComboBox绑定到Inspector表的新SQL Server视图.

SELECT NULL as inspector_id, '(none)' as fullName, '' as employeeCode
UNION
SELECT inspector_id, fullName, employeeCode
FROM dbo.inspector

专利:

  1. (none)项目在ComboBox
  2. 选择项目时的SelectedItem和文本持续存在.
  3. SQL视图允许inspector_id
  4. 的空值
  5. 在应用程序代码中不需要解决方法.只需从视图中填充DataSet
  6. 允许更多的灵活性,因为这种关系不受限制.

...辉煌!

其他推荐答案

尝试各种方法后,我最初决定以下内容:

private void ItemInfo_Load(object sender, EventArgs e)
{
    this.inspectorTableAdapter.Fill(this.someDBDataSet.inspector);
    this.itemTableAdapter.Fill(this.someDBDataSet.item);
}

private void noInspector_btn_Click(object sender, EventArgs e)
{
    inspector_idComboBox.SelectedItem = null;
}

我添加了一个(链接)按钮以清除ComboBox.

,而不是在ComboBox中添加虚拟项目

专利:

  1. ComboBox清除.
  2. tableAdapter设置item.inspector_id = NULL

cons:

  1. 其他形式控件绑定到inspector字段保持不变(因为没有"空" inspector行使用).
  2. inspector_idComboBox SelectedItem为null时,没有显示文本.我更喜欢在框中显示(none)之类的东西.

其他推荐答案

另一种方法是选择(none)时清除ComboBox:

private void inspector_idComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
    if (inspector_idComboBox.SelectedValue != null)
        if ((int)inspector_idComboBox.SelectedValue == -1)
            inspector_idComboBox.SelectedItem = null;
}

专利:

  1. 正确的空值保存到数据集并发送到数据库.
  2. 不需要外部清除按钮.

cons:

  1. 选择(none)还清除了文本.我更喜欢(none)保持选择.

本文地址:https://www.itbaoku.cn/post/25823.html

问题描述

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:

  1. The (none) item is in the ComboBox
  2. The SelectedItem and text persists when selecting the item.
  3. The SQL view allows a NULL value for inspector_id
  4. No workarounds are needed in the application code. Just fill the DataSet from the view.
  5. Allows more flexibility as the relationship is not bound.

... brilliant!

其他推荐答案

After trying various approaches, I initially decided on the following:

private void ItemInfo_Load(object sender, EventArgs e)
{
    this.inspectorTableAdapter.Fill(this.someDBDataSet.inspector);
    this.itemTableAdapter.Fill(this.someDBDataSet.item);
}

private void noInspector_btn_Click(object sender, EventArgs e)
{
    inspector_idComboBox.SelectedItem = null;
}

Rather than adding a dummy item into the ComboBox, I added a (link) button to clear the ComboBox.

Pros:

  1. The ComboBox clears.
  2. The tableAdapter sets item.inspector_id = NULL

Cons:

  1. Other form controls bound to inspector fields remain unchanged (as there is no "empty" inspector row to use).
  2. No text is displayed in inspector_idComboBox when SelectedItem is null. I'd prefer having something like (none) shown in the box.

其他推荐答案

Another approach is to clear the ComboBox when selecting (none):

private void inspector_idComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
    if (inspector_idComboBox.SelectedValue != null)
        if ((int)inspector_idComboBox.SelectedValue == -1)
            inspector_idComboBox.SelectedItem = null;
}

Pros:

  1. The correct NULL value is saved to the DataSet and sent on to the database.
  2. No external clear button is needed.

Cons:

  1. Selecting (none) also clears the text. I'd prefer for (none) to stay selected.