Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我会在一周中的某一天崩溃?_C#_Datagridview_Switch Statement_Datagridviewrow - Fatal编程技术网

C# 为什么我会在一周中的某一天崩溃?

C# 为什么我会在一周中的某一天崩溃?,c#,datagridview,switch-statement,datagridviewrow,C#,Datagridview,Switch Statement,Datagridviewrow,我的系统时钟是2014年9月16日(星期二) 但在代码中,我总是跳到星期一 DayOfWeek dow = new DateTime().DayOfWeek; int columnNumber = 0; columnNumber = columnNumber + 0; foreach ( DataGridViewRow row in dataGridView1.Rows ) { switch ( dow ) { case DayOfWeek.Monday: columnN

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

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

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列为
    文本
  • 列4-9为
    DataGridViewCheckBoxColumn
new DateTime()
不提供今天的日期,而是提供
DateTime的默认值

您想将该行更改为:

DayOfWeek-dow=DateTime.Now.DayOfWeek

new DateTime()
不提供今天的日期,而是提供
DateTime的默认值

您想将该行更改为:


DayOfWeek-dow=DateTime.Now.DayOfWeek

我回答了一个问题。你应该每个问题问一个问题。请下次阅读。
DateTime
的第一个示例显示
newdatetime()
默认为
1/1/0001 12:00:00 AM.
甚至只使用调试器。不需要花太多的精力就可以注意到您正在创建的
DateTime
对象不是今天的日期。您可能应该尝试使用LINQ。我回答了一个问题。你应该每个问题问一个问题。请下次阅读。
DateTime
的第一个示例显示
newdatetime()
默认为
1/1/0001 12:00:00 AM.
甚至只使用调试器。不需要花太多的精力就可以注意到您正在创建的
DateTime
对象不是今天的日期。您可能应该尝试使用LINQ。