- System.NullReferenceException错误

时间:2014-04-18 15:28:58

标签: c# datagridview nullreferenceexception

这是我今天第二次看到此错误消息。我得到了一些帮助earlier on,但现在似乎发生了我写的日期比较功能。

每当用户按下Overdue按钮时,datagridview中的所有行都需要按第1列(DatePaid)更新。

enter image description here

不幸的是,这似乎不起作用,我收到错误消息:

An unhandled exception of type 'System.NullReferenceException' occurred in SpeedyRent.exe

Additional information: Object reference not set to an instance of an object.

在:

DateTime dateRow = DateTime.Parse(row.Cells[0].Value.ToString());

enter image description here

我的代码是:

    public void viewOverdue_Click(object sender, EventArgs e)
    {
        viewOverdue.ForeColor = Color.Red;
        viewHire.ForeColor = Color.Black;
        viewRent.ForeColor = Color.Black;

        DateTime overdueDate = default(DateTime);
        DateTime today = DateTime.Now;
        string odDate = null;

        if (today.DayOfWeek == DayOfWeek.Monday)
        {
            overdueDate = today.AddDays(-12);
            odDate = overdueDate.Date.ToString("dd/MM/yyyy HH:mm:ss");
        }
        else if (today.DayOfWeek == DayOfWeek.Tuesday)
        {
            overdueDate = today.AddDays(-13);
            odDate = overdueDate.Date.ToString("dd/MM/yyyy HH:mm:ss");
        }
        else if (today.DayOfWeek == DayOfWeek.Wednesday)
        {
            overdueDate = today.AddDays(-7);
            odDate = overdueDate.Date.ToString("dd/MM/yyyy HH:mm:ss");
        }
        else if (today.DayOfWeek == DayOfWeek.Thursday)
        {
            overdueDate = today.AddDays(-8);
            odDate = overdueDate.Date.ToString("dd/MM/yyyy HH:mm:ss");
        }
        else if (today.DayOfWeek == DayOfWeek.Friday)
        {
            overdueDate = today.AddDays(-9);
            odDate = overdueDate.Date.ToString("dd/MM/yyyy HH:mm:ss");
        }
        else if (today.DayOfWeek == DayOfWeek.Saturday)
        {
            overdueDate = today.AddDays(-10);
            odDate = overdueDate.Date.ToString("dd/MM/yyyy HH:mm:ss");
        }
        else if (today.DayOfWeek == DayOfWeek.Sunday)
        {
            overdueDate = today.AddDays(-11);
            odDate = overdueDate.Date.ToString("dd/MM/yyy HH:mm:ss");
        }

        CurrencyManager manager = (CurrencyManager)BindingContext[dataGridView1.DataSource];
        manager.SuspendBinding();
        DateTime dateBase = DateTime.Parse(odDate);
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            DateTime dateRow = DateTime.Parse(row.Cells[0].Value.ToString());
            row.Visible = (dateRow <= dateBase);
        }
        manager.ResumeBinding();
    }

0 个答案:

没有答案