首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

请问下大家为空的时候怎么办

2013-10-19 
请教下大家为空的时候怎么处理类型有stringintbool这个几个比较有代表吧我模型里面是这么做的public DateT

请教下大家为空的时候怎么处理
类型有  string   int   bool   这个几个比较有代表吧

我模型里面是这么做的
public DateTime? Approvedate { get; set; }
public String Picture { get; set; }
public Boolean? Planstatus { get; set; }

然后读到datagridview 中  有些直接在数据库中是空的null
点击datagridview之后  对每个单元格进行读取  再在下面text控件显示出来 为了方便我做了个类型转换的类
里面的方法为:(请大家帮我看下下面行不行)
 public static int? convertint(string intvalue)
        {
            int? typeint=null;
            if (intvalue == null || intvalue =="")
            {
                typeint = null;
            }
            else
            {
                typeint = Convert.ToInt32(intvalue);
            }
            return typeint;
        }
        public static DateTime? convertdatetime(string datetimevalue)
        {
            DateTime? typedatetime = null;
            if (datetimevalue == null || datetimevalue =="")
            {
                typedatetime = null;
            }
            else
            {
                typedatetime = Convert.ToDateTime(datetimevalue);
            }
            return typedatetime;
        }
        public static bool? convertbool(string boolvalue)
        {
            bool? typebool = null;
            if (boolvalue == null || boolvalue == "")
            {
                typebool = null;
            }
            else
            {
                typebool = Convert.ToBoolean(boolvalue);
            }
            return typebool;
        }


最终控件显示还是有问题  应该怎么写这个方法  大家怎么做的
[解决办法]
return intvalue==null?0:intvalue

热点排行