为什么我崩溃了并且一周中出现错误的一天? [英] Why am I crashing and getting the wrong day of the week?

查看:110
本文介绍了为什么我崩溃了并且一周中出现错误的一天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的系统时钟为2014年9月16日(星期二)

My system clock is 9/16/2014 (Tuesday)

但是在代码中,我总是跳到星期一.

But in code, I'm always jumping to Monday.

DayOfWeek dow = new DateTime().DayOfWeek;
int columnNumber = 0;

columnNumber = columnNumber + 0;

foreach ( DataGridViewRow row in dataGridView1.Rows )
{
  switch ( dow )
  {
  case DayOfWeek.Monday:
    columnNumber = 4;
    if ( (bool) row.Cells[4].Value == true ) // crashing here with NullReferenceException
    {
      row.Cells["activeTodayDataGridViewCheckBoxColumn"].Value = true;
    }
    break;

我有一个DataGridView

  • 0至3列是Text
  • 第4列至第9列是DataGridViewCheckBoxColumn
  • Columns 0—3 are Text
  • Columns 4—9 are DataGridViewCheckBoxColumn

推荐答案

new DateTime()没有提供今天的日期,但是DateTime

new DateTime() isn't providing today's date, but the default value for DateTime

您想将该行更改为:

DayOfWeek dow = DateTime.Now.DayOfWeek;

这篇关于为什么我崩溃了并且一周中出现错误的一天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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