c# - 使用 C# 中的 datagridview 值将表中的状态从 "reserved"更新为 "expired"

标签 c# mysql datagridview cell-formatting

您好,先生。我正在开发一个 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/

相关文章:

c# - 权限拒绝 : requires android. permission.READ_PHONE_STATE

c# - 在用作 MetadataTypeAttribute 中的类型的类上使用 DefaultValueAttribute

c# - OpenFileDialog C#,允许用户输入文件名?

MySQLi登录脚本显示空白页面

mysql - 左连接查询问题

c# - 获取datagridview最后一行索引

c# - 如何在 VBA 和 C# 中编码/解码 XML

mysql - 用于将一列与另一列进行比较的 SQL 查询

c# - 在datagridview c#中使一个单元格可编辑

c# - 在 DataGridView 中排列列