DataGridCell empty set DbNull.value

2019-07-21 18:31发布

I am populating a datagrid with a dataset and I have my dataset verified for nullable values -

My problem is :

When the dataset is validating, it found empty rows and the errors are not displayed. I would like to define the empty cells with a DBNull.value. Is there any way to do it ?. I have found a property named TargetNullValue that could work ?

标签: c# wpf mvvm
1条回答
贪生不怕死
2楼-- · 2019-07-21 18:46

I found a way to do it, by making a convertion

 public class ConvertStringToDBNull : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo cufo)
    {

        if (value is string)
        {
            if (value.ToString() == string.Empty)
            {
                return DBNull.Value;
            }
        }
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo cufo)
    {

        if (value is string)
        {
            if (value.ToString() == string.Empty)
            {
                return DBNull.Value;
            }
        }
        return value;
    }

}
查看更多
登录 后发表回答