您好,先生。我正在开发一个 datagridview,我希望它在超过到期日期时自动更新其行以及数据库表状态从“保留”到“过期”。 我正在使用 DataGridViewCellFormattingEventArgs 来确定条件并更新数据库。这是我的代码:
void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{if (DateTime.Now.Date > Convert.ToDateTime(row.Cells[7].Value).Date ))
{
con = new SqlConnection(conString);
string query = "UPDATE tblReservation SET Status='expired'";
cmd = new SqlCommand(query,con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
但是每当我执行这些代码时。它将表的所有状态设置为“已过期”。用于解决此问题的正确方法或正确事件是什么?。
预先感谢您的帮助。
最佳答案
您需要在 sql 语句中添加一个 WHERE 子句,就像 @rene 在他的评论中建议的那样。
Sql语句示例:
UPDATE tblReservation
SET Status='expired'
WHERE reservationId = row.Cells[***the row id index should come here***].Value;
关于c# - 使用 C# 中的 datagridview 值将表中的状态从 "reserved"更新为 "expired",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29059891/