不能隐式转换类型 'system.datetime' 顶级字符串 c#

cannot implicitly convert type 'system.datetime' top string c#

我正在尝试创建警报系统,但收到此错误:无法隐式转换类型 'system.datetime' top string c#

我的代码:

private void button1_Click(object sender, DataGridViewCellFormattingEventArgs e)
    {
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            string dateInString = DateTime.Parse(dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString()); //error
        }
     DateTime startDate = DateTime.Parse(dateInString);
     DateTime expiryDate = startDate.AddDays(30);

     if (DateTime.Now > expiryDate)
      {
         textBox3.Text = "O pagamento X expirou, faça o favor de pagar, caloteiro";
    }
    }

请帮忙。 谢谢。

问题出在以下行:

string dateInString = DateTime.Parse(dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString());

您正在尝试将 DateTime 值赋给 string 变量。

相反,只需对对象进行 .ToString() 小调用即可获取原样的值:

string dateInString = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();

除此之外,您还有一些作用域问题,您实际上指的是最后分配的 DateTime 值:

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    string dateInString = DateTime.Parse(dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString()); //error
}
DateTime startDate = DateTime.Parse(dateInString);