如何设置等于在RowUpdating为空字段值(...) [英] How to set field values equal to null in RowUpdating(...)

查看:211
本文介绍了如何设置等于在RowUpdating为空字段值(...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的code:

    protected void ConfigurationGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        // row's Update button is clicked, but before the GridView control 
        // updates the row
        DropDownList ddlst = 
           ((DropDownList)ConfigurationGridView.Rows[e.RowIndex].FindControl("SubUnitDropDownList"));

        if (ddlst != null)
        {
            ListItem li = ddlst.SelectedItem;

            if (li != null)
            {
                if (Convert.ToInt32(li.Value) == -1)
                {
                    // next line is where the problem is
                    // null doesn't change the old value
                    // it does nothing, how to I change this value to null??
                    e.NewValues["SubUnitId"] = null;
                }
                else
                {
                    // we set the actual value
                    // this works nicely
                    e.NewValues["SubUnitId"] = li.Value;
                }
            }
        }
    }

基本上,我有一个字段,它通过组合框获取其值,在组合的第一个值重新presents未分配的/不适用/不设置值,或者该记录的空值,它的工作原理时,我指定其他值,例如1,2或3,当组合得-1我要清除该值,即把空值,但线之上似乎并没有做任何事情,我已经试过可空< INT32>中的Int32?值= NULL,Convert.ToInt32(空)其实是把0,而不是空。
当我尝试System.DBNull.Value我得到一个错误:
    System.DBNull'类型的对象''不能转换为类型'System.Nullable`1 [System.Int32]'。

Basically, I have one field that gets its value via a combo box, the first value in the combos represents the unassigned/not applicable/not set value, or the null value for that record, it works when I assign other values, say 1, 2, or 3, when the combo gets -1 I want to clear this value i.e. put the null value, but the line above doesn't seem to do anything, I've tried Nullable<int32>, Int32? value = null, Convert.ToInt32(null) actually puts 0 instead of null. When I try System.DBNull.Value I get an error: 'Object of type 'System.DBNull' cannot be converted to type 'System.Nullable`1[System.Int32]'.'.

我是什么做错了吗?在此先感谢您的输入。

What am I doing wrong? thanks in advance for your input.

推荐答案

也许你可以试试这个:

e.NewValues["SubUnitId"]=System.DBNull.Value

这篇关于如何设置等于在RowUpdating为空字段值(...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆