如何清除 (WPF) DataGrid 中的单元格(使其获取 NULL 值)?

发布于 2025-01-14 17:38:58 字数 1135 浏览 4 评论 0原文

我们有一个列绑定到一个可为空的 SQL 整数值。当用户尝试“清除”DataGrid 中的单元格时,我们收到验证错误

“...无法转换”。

如何将其设置为空并将“空”值绑定到基础列?

我已经用谷歌搜索了这个问题两天并找到了任何东西。我试图编辑我对 HCL 的评论,但它永远不会保存。

我熟悉 IValueConverters。我写了一个来处理我们的百分比(这样用户可以输入“50%”,它会转换为“.5”)。我正在尝试做一些非常类似的事情,但是当该值作为 Null 返回时(我编写了一个虚拟的 IValueConterter 来调试它),我只想保存它作为 Null 发送到数据库。也许我只需要把它设置为DBNullValue?对于我认为可能具有内置属性的东西来说,这似乎需要做很多工作。我尝试在列 (DataGridBoundColumn) 上使用 TargetNullValue 属性并将其设置为 DBNull.Value,但它没有更改该值 (基于我的虚拟 IValueConverter,它仍然作为常规(字符串)NULL 输入,

我只是希望能够为此将空值保存到数据库中(整数) 。类型) ** 最新补充

**

好点 HCL,我忘记了有时

我正在绑定到 SQL 表。 >NULL 列

是使用 AutoGenerateColumn 创建的,因此它基本上只是“自动”连接它们(此方法中应用了某些样式,例如右对齐,但不适用于此列)它采用这种方法,因为该应用程序非常多我们的 CIO 要求我们从用户中删除 Access,所以这就是解决方案(创建我们自己的 MS Access),因为它可以打开任何表(并创建表、列等),它只是“自动生成”。 " 基于打开的表的列。尽管如此,由于应用程序本身就知道某些列,例如 Discount_Pct,因此当它遇到这些列之一时,它确实会执行一些“特殊操作”(例如分配我上面描述的 IValueConverter)。虽然,就像我说的……对于这个特定的专栏,没有做任何“特别”的事情。它只是一个“自动生成”的常规 SQL 整数(可为空)。

We have a column bound to a sql integer value that is nullable. When the user tries to "clear" the cell in the DataGrid, we get the validation error

"...could not be converted".

How can I make it empty and bind a "null" value to the underlying column?

I have googled this problem for 2 days now and found anything. I tried to edit my comment to you HCL, but it would never save.

I am familiar with the IValueConverters. I wrote one to handle our percentages (so the user can enter "50 %" and it gets converted to ".5"). I am trying to do something very similar, but when the value comes back as a Null (I wrote a dummy IValueConterter to debug it), I just want it to be saved to the database as a Null. Maybe I just need to set it to DBNullValue? It just seems like a lot of work for something that I think might have a built in property. I tried using the TargetNullValue property on the column (DataGridBoundColumn) and setting that to DBNull.Value, but it didn't change the value (based on my dummy IValueConverter it is still coming in as a regular (string) NULL.

I simply want to be able to save a null value to the database for this (integer type) column.

** Latest addition **

Good point HCL, I forget that the whole world isn't ADO sometimes.

I am binding to a SQL table. The column in question is an int that allows NULLs.

The columns are created using AutoGeneratingColumn, so it basically just hooks them up "automagically" (certain styles such as Right Justify are applied in this method, but not for this column). It takes this approach, because the app is pretty much an "Access" replacement. Our CIO mandated we remove Access from the users, so this was the solution (create our own MS Access). Anyway, since it can open any table (and create tables, columns, etc) it just "AutoGenerates" the columns based on the table that was opened. Although, since there are certain columns that the app inherently knows about such as Discount_Pct, when it encounters one of those columns, it does do some "special stuff" (like assign the IValueConverter I described above). Although, like I said... for this particular column, there isn't anything "special" done to it. It is just a regular SQL integer (nullable) that is "AutoGenerated".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

-残月青衣踏尘吟 2025-01-21 17:38:58

我刚刚找到了我正在寻找的房产!

TargetNullValue

这篇文章解释了它:

问题是,这些列是使用“AutoGenerateColumns”生成的(因为数据源...一个sql表... “动态的”)。如果我能弄清楚如何获取表列(原始)类型,我已经知道如何查看它是否为 Nullable,那么我可以为该列设置此值。 (我仍然不明白为什么 AutoGenerateColumns 不能自动处理这个问题!!)

I just found the property I was looking for!!

TargetNullValue

This post explains it: HERE

The problem is, these columns are generated using "AutoGenerateColumns" (because the datasource...a sql table... is "dynamic"). If I could figure out how to get the table column (primitive) type, I already know how to see if it is Nullable, then I can set this value for that column. (I still don't understand why the AutoGenerateColumns doesn't handle this automatically!!)

旧话新听 2025-01-21 17:38:58

延伸 Shayne 的自我回答,我发现 TargetNullValue 属性在 DataGridBoundColumn 对象上可用。您可以使用描述的附加逻辑 从此评论中将此作为条件。不幸的是,那篇文章没有解释如何将其逻辑与 DataGrid 关联起来,所以这是我的解决方案:

<DataGrid AutoGenerateColumns="True" AutoGeneratingColumn="m_grid_AutoGeneratingColumn"/>

并且......

public void m_grid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    // Make all columns nullable
    ((DataGridBoundColumn)e.Column).Binding.TargetNullValue = string.Empty;
}

我对这个问题感到非常沮丧。我希望这对某人有帮助。

Extending off of Shayne's self-answer, I discovered that the TargetNullValue property is available on DataGridBoundColumn objects. You could use the additional logic described from this comment to make this conditional. Unfortunately, that post does not explain how to relate its logic to a DataGrid, so here is my solution:

<DataGrid AutoGenerateColumns="True" AutoGeneratingColumn="m_grid_AutoGeneratingColumn"/>

And...

public void m_grid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    // Make all columns nullable
    ((DataGridBoundColumn)e.Column).Binding.TargetNullValue = string.Empty;
}

I was so frustrated with this problem. I hope this helps someone.

作业与我同在 2025-01-21 17:38:58

我也决定在这个领域采用 IValueConverter 路线。

这是我写的(并且它有效,我仍然觉得必须有一个更简单的方法!哈哈):

    [ValueConversion(typeof(Int32), typeof(String))]
    public class IntDBNullConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return "";
            else if (DBNull.Value.Equals(value))
                return "";
            else
                return value.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string str = value as string;
            if (String.IsNullOrEmpty(str))
                return DBNull.Value; // returns DBNull.Value 

            Int32 result = 0;

            Int32.TryParse(str, out result);

            return result;
        }
    }

I decided to go the IValueConverter route for this field too.

Here is what I wrote (and it works, I still feel like there must be an easier way! lol):

    [ValueConversion(typeof(Int32), typeof(String))]
    public class IntDBNullConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return "";
            else if (DBNull.Value.Equals(value))
                return "";
            else
                return value.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string str = value as string;
            if (String.IsNullOrEmpty(str))
                return DBNull.Value; // returns DBNull.Value 

            Int32 result = 0;

            Int32.TryParse(str, out result);

            return result;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文