0

先生,您好。我正在研究一个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();
                    }
            }
        }

但是每当我执行此代码时。它将表的所有状态设置为“过期”。用于解决此问题的正确方法或正确事件是什么?

提前感谢您的帮助。

4

1 回答 1

0

您需要在 sql 语句中添加一个 WHERE 子句,如他的评论中建议的@rene。

sql语句示例:

UPDATE tblReservation 
SET Status='expired' 
WHERE reservationId = row.Cells[***the row id index should come here***].Value;
于 2015-03-15T11:26:16.887 回答