Why am I crashing and getting the wrong day of the week?

余生长醉 提交于 2019-12-25 16:39:59

问题


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;

I have a DataGridView

  • Columns 0—3 are Text
  • Columns 4—9 are DataGridViewCheckBoxColumn

回答1:


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

You want to change that line to :

DayOfWeek dow = DateTime.Now.DayOfWeek;



来源:https://stackoverflow.com/questions/25874523/why-am-i-crashing-and-getting-the-wrong-day-of-the-week

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!