当前位置:编程学习 > C#/ASP.NET >>

时间判断问题!求达人!

我数据库中有datetime数据,可空的,做一个到期提醒,但是提醒时会出现DBNULL错误,我是这样写的
DateTime nowtime = DateTime.Now;
                            DateTime time = Convert.ToDateTime(dataGridView1.Rows[i].Cells[j].Value);
                            TimeSpan ts1 = new TimeSpan(nowtime.Ticks);
                            TimeSpan ts2 = new TimeSpan(time.Ticks);

                            if (ts2 < ts1)
                            {
                                this.dataGridView1.Rows[i].Cells[j].Style.BackColor = Color.Red;
                            }
然后我用tryparse,
DateTime t3;
                            DateTime nowtime = DateTime.Now;
                            DateTime time = DateTime.TryParse(Convert.ToDateTime(dataGridView1.Rows[i].Cells[j].Value).ToString(),out t3);
                            TimeSpan ts1 = new TimeSpan(nowtime.Ticks);
                            TimeSpan ts2 = new TimeSpan(time.Ticks);

                            if (ts2 < ts1)
                            {
                                this.dataGridView1.Rows[i].Cells[j].Style.BackColor = Color.Red;
                            }
提示无法从类型“bool”隐士转换为system。datetime,
这该如何解决呢 
我本来是想先判断 如果单元格中的值为null就跳过 就是i++;但是提示超出数组界限!求高手解答下!在线等! --------------------编程问答-------------------- Convert.ToDateTime(dataGridView1.Rows[i].Cells[j].Value);这里面有bool类型的数据被,true或者false 这样的怎么转成2011-02-02这样的,数据类型都不一致,天方夜谭 --------------------编程问答--------------------
引用 1 楼 loveyan52152112 的回复:
Convert.ToDateTime(dataGridView1.Rows[i].Cells[j].Value);这里面有bool类型的数据被,true或者false 这样的怎么转成2011-02-02这样的,数据类型都不一致,天方夜谭

就因为有问题才请教的 大侠! --------------------编程问答-------------------- 晕死,bool类型还转个毛啊,直接更新数据库吧 --------------------编程问答-------------------- 不能随便执行I++,首先检查字段是否为空,不为空再尝试转化为时间类型 --------------------编程问答-------------------- 读数据的时候加个条件不就可以了datetime is not null   治根了吧 --------------------编程问答-------------------- DateTime time = DateTime.TryParse(Convert.ToDateTime(dataGridView1.Rows[i].Cells[j].Value).ToString(),out t3);
这句不对,TryParse返回的是bool类型 --------------------编程问答-------------------- 用DBNull.Value进行判断 --------------------编程问答-------------------- bool flag = DateTime.TryParse(Convert.ToDateTime(dataGridView1.Rows[i].Cells[j].Value).ToString(),out t3); --------------------编程问答-------------------- if(dataGridView1.Rows[i].Cells[j].Value) {
time="0000-00-00"
}else{
time = Convert.ToDateTime(dataGridView1.Rows[i].Cells[j].Value);
};
加个判断! --------------------编程问答-------------------- 这个直接try抛出异常就实现功能了吧。。。。 --------------------编程问答-------------------- 然后出错和不正常数据你看是该覆盖还是该怎样都可以啊。。。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,