【问题标题】:Show null value (Guid) in Datagrid when value in database is NULL当数据库中的值为 NULL 时,在 Datagrid 中显示空值(Guid)
【发布时间】:2012-11-29 13:09:53
【问题描述】:

在我的数据库表中,我有一列可以包含 Guid,但也可以是 NULL。 在我的 Viewmodel 中,此字段配置为 Guid 并绑定到我的 DataGrid

当我清除 DataGrid 中的字段并因此将值设置为 null 时,我收到错误“无法识别的 Guid 格式”。这是正确的,因为 guid 不能为空。 但是如何将其显示为 null 或至少将其作为 NULL 传递给数据库?

XAML 绑定:

<DataGridTextColumn Binding="{Binding AttributeTypeValueId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="AttributeTypeValueId" />

还有我在模型中的属性:

private Guid _attributeTypeValueId;

public Guid AttributeTypeValueId
{
    get 
    {
        return _attributeTypeValueId; 
    }

    set
    {
        if (value != _attributeTypeValueId)
        {
            _attributeTypeValueId = value;
            RaisePropertyChanged("AttributeTypeValueId");
        }
    }
}

知道我该怎么做吗?

谢谢!

【问题讨论】:

  • 尝试将其设置为 Guid.Empty
  • 或者尽可能使用 Nullable

标签: c# wpf datagrid viewmodel


【解决方案1】:

您可以使用可为空的:

private Guid? _attributeTypeValueId;

【讨论】:

  • 嗯,即使我将 prop 更改为 Guid,我仍然会收到错误消息?我需要更改我的 xaml 绑定中的任何内容吗?应该可以将数据网格中的字段留空。感谢您的快速回复!
【解决方案2】:

将字段和属性的类型更改为 Guid?应该可以。我刚刚测试过。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    相关资源
    最近更新 更多